优化这段代码title = soup.find('h2', {'class': 'article-title'}).text.strip()
时间: 2023-09-04 22:08:54 浏览: 86
这段代码本身已经很简洁了,但是如果你想要优化它,可以将键名和类名改为变量,这样可以提高代码的灵活性和可重用性。例如:
```
class_name = 'article-title'
title_tag = 'h2'
title = soup.find(title_tag, {'class': class_name}).text.strip()
```
这样,如果你想要找到其他类名为 `article-title` 的标签,只需要修改 `class_name` 变量即可。同样,如果你想要找到其他标签类型的 `article-title`,只需要修改 `title_tag` 变量即可。
相关问题
当使用这段代码时title = soup.find('h2', {'class': 'article-title'}).text.strip()出现错误AttributeError: 'NoneType' object has no attribute 'text'
这种错误通常是因为 `soup.find()` 没有找到匹配的元素,返回了 `None` 对象。在这种情况下,你尝试对 `None` 对象调用 `text` 属性,就会出现 `'NoneType' object has no attribute 'text'` 的错误。
为了避免这种错误,你可以先使用 `if` 语句检查 `soup.find()` 是否返回了有效的元素,如果返回了 `None` 对象,则需要进行错误处理。例如:
```
title_tag = soup.find('h2', {'class': 'article-title'})
if title_tag:
title = title_tag.text.strip()
else:
title = 'No title found'
```
在这个例子中,我们首先使用 `soup.find()` 查找 `h2` 标签,并且类名为 `article-title`。然后,我们使用 `if` 语句检查 `title_tag` 是否为 `None` 对象。如果 `title_tag` 不是 `None` 对象,则使用 `title_tag.text.strip()` 提取标题文本;否则,设置 `title` 为字符串 `'No title found'`。这样,即使 `soup.find()` 没有找到匹配的元素,程序也不会崩溃,而是会输出一个友好的错误提示。
import requests from bs4 import BeautifulSoup import pprint import json import time def download_all_htmls(): """下载所有列表页面的HTML,用于后续的分析""" htmls = [] headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'} for idx in range(24): url = f"https://www.utusan.com.my/page/{idx+1}" print("craw html:", url) r = requests.get(url) if r.status_code != 200: raise Exception("error") htmls.append(r.text) with open(f"page{idx+1}.txt", "w", encoding="utf-8") as f: f.write(r.text) if (idx+1) % 20 == 0: print("Sleep for 10 seconds...") time.sleep(10) return htmls htmls = download_all_htmls() for idx, html in enumerate(htmls): soup = BeautifulSoup(html, 'html.parser') articles = soup.find_all('article') for article in articles: title = article.find('h2').get_text().strip() content = article.find('div', {'class': 'field-item even'}).get_text().strip() with open(f"page{idx+1}_{title}.txt", "w", encoding="utf-8") as f: f.write(content)这段代码爬取网站的每一个链接,都只能停留在首页,请改进这段代码,让它能够爬取500个链接的页面
可以将代码改进为循环遍历每一页,并在每一页中获取所有的文章链接,再逐个爬取每个链接的内容,直到爬取500个链接为止。具体实现如下:
```
import requests
from bs4 import BeautifulSoup
import pprint
import json
import time
def download_all_articles():
"""下载所有文章的内容"""
articles = []
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'}
for idx in range(25):
url = f"https://www.utusan.com.my/page/{idx+1}"
print("craw html:", url)
r = requests.get(url)
if r.status_code != 200:
raise Exception("error")
soup = BeautifulSoup(r.text, 'html.parser')
article_links = soup.find_all('a', {'class': 'node-title'})
for link in article_links:
article_url = link.get('href')
print("craw article:", article_url)
r = requests.get(article_url)
if r.status_code != 200:
raise Exception("error")
soup = BeautifulSoup(r.text, 'html.parser')
title = soup.find('h1', {'class': 'title'}).get_text().strip()
content = soup.find('div', {'class': 'field-item even'}).get_text().strip()
articles.append({'title': title, 'content': content})
if len(articles) == 500:
return articles
time.sleep(1)
return articles
articles = download_all_articles()
pprint.pprint(articles)
```
这段代码会遍历每一页,并获取每一页中所有文章的链接。然后,依次爬取每个链接的内容,并保存到一个列表中,直到爬取500个链接为止。注意,为了避免过于频繁的访问网站,代码中添加了一秒钟的等待时间。
阅读全文
相关推荐















