我正试图打印出《密歇根日报》阅读量最高的文章中所有文章标题的列表,如意见页上所示,并用一行空行隔开每一篇文章的标题。
这是我现在写的内容,但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
,类"view content"包含span
的(类"field content"),其中嵌入了该部分的文章链接。第三个"查看内容"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()