url = "http://www.greenfinance.org.cn/" # 发送HTTP请求,获取网页HTML代码 response = requests.get(url) html = response.content # 使用BeautifulSoup解析HTML代码 soup = BeautifulSoup(html, 'html.parser') # 查找包含政策数据的HTML元素 policy_section = soup.find('div', class_='policy-section') # 查找政策标题和内容 policy_titles = policy_section.find_all('h3') policy_contents = policy_section.find_all('p') print(policy_section) # 输出政策数据 for i in range(len(policy_titles)): print(policy_titles[i].text) print(policy_contents[i].text) print() Traceback (most recent call last): File "/var/folders/wj/mvzl124x2xv1ywq89bjh3qkm0000gn/T/ipykernel_71039/3485610234.py", line 14, in <module> policy_titles = policy_section.find_all('h3') AttributeError: 'NoneType' object has no attribute 'find_all' url = "http://www.greenfinance.org.cn/" # 发送HTTP请求,获取网页HTML代码 response = requests.get(url) html = response.content # 使用BeautifulSoup解析HTML代码 soup = BeautifulSoup(html, 'html.parser') # 查找包含政策数据的HTML元素 policy_section = soup.find('div', class_='policy-section') # 打印policy_section的值 print(policy_section) # 查找政策标题和内容 policy_titles = policy_section.find_all('h3') policy_contents = policy_section.find_all('p') # 输出政策数据 for i in range(len(policy_titles)): print(policy_titles[i].text) print(policy_contents[i].text) print() None Traceback (most recent call last): File "/var/folders/wj/mvzl124x2xv1ywq89bjh3qkm0000gn/T/ipykernel_71039/3956965668.py", line 17, in <module> policy_titles = policy_section.find_all('h3') AttributeError: 'NoneType' object has no attribute 'find_all'
时间: 2024-04-03 18:33:14 浏览: 68
python爬虫练习:爬图片
这段代码的问题是在查找包含政策数据的HTML元素时,返回的结果为None,即没有找到该元素。因此在之后的代码中,访问None的属性或方法时,会出现'NoneType' object has no attribute 'find_all'的错误提示。可能的原因是该网页的结构发生了变化或者网络连接出现了问题,建议检查一下网页的结构和网络连接状态。
阅读全文