Python 2.7 如何使用beautifulsoup打印所有文章标题的列表
我正试图打印出《密歇根日报》阅读最多文章的所有文章标题的列表,如图所示,并在每个文章标题处留出一行空白 这就是我现在写的内容,但是Python 2.7 如何使用beautifulsoup打印所有文章标题的列表,python-2.7,beautifulsoup,Python 2.7,Beautifulsoup,我正试图打印出《密歇根日报》阅读最多文章的所有文章标题的列表,如图所示,并在每个文章标题处留出一行空白 这就是我现在写的内容,但是class=“field content”不够窄,无法在阅读量最大的框中仅获取标题 import requests from bs4 import BeautifulSoup base_url = 'http://www.michigandaily.com/section/opinion' r = requests.get(base_url) soup = Be
class=“field content”
不够窄,无法在阅读量最大的框中仅获取标题
import requests
from bs4 import BeautifulSoup
base_url = 'http://www.michigandaily.com/section/opinion'
r = requests.get(base_url)
soup = BeautifulSoup(r.text, "html5lib")
for story_heading in soup.find_all(class_="field-content"):
if story_heading.a:
print(story_heading.a.text.replace("\n", " ").strip())
# else:
# print(story_heading.contents[0].strip())
非常感谢您的帮助:)文章分为三个部分。每个都是一个
div
,类“查看内容”包含span
(类“字段内容”)嵌入该节的文章链接。第三个“查看内容”div包含“阅读最多”的文章。以下内容应通过扫描第三个(“最常阅读”)div中的“字段内容”来检索这些文章:
mostReadSection = soup.findAll('div', {'class':"view-content"})[2] # get the most read section
storyHeadings = mostReadSection.findAll('span', {'class':"field-content"})
for story_heading in storyHeadings:
if story_heading.a:
print story_heading.a.text.replace("\n", " ").strip()
下面的答案对你有用吗?是的,非常感谢!