2016-04-14 42 views
1

我想提取新聞頁面的最多讀取部分中的標題。這是我到目前爲止,但我得到的所有標題。我只是想要閱讀最多的部分。用BS4提取大多數讀取標題

`

import requests 
from bs4 import BeautifulSoup 

base_url = 'https://www.michigandaily.com/section/opinion' 
r = requests.get(base_url) 
soup = BeautifulSoup(r.text, "html5lib") 

for story_heading in soup.find_all(class_= "views-field views-field-title"): 
    if story_heading.a: 
     print(story_heading.a.text.replace("\n", " ").strip()) 
    else: 
     print(story_heading.contents[0].strip())` 

回答

1

您需要將範圍限制爲只爲最閱讀文章的DIV容器。

import requests 
from bs4 import BeautifulSoup 

base_url = 'https://www.michigandaily.com/section/opinion' 
r = requests.get(base_url) 
soup = BeautifulSoup(r.text, "html5lib") 

most_read_soup = soup.find_all('div', {'class': 'view-id-most_read'})[0] 

for story_heading in most_read_soup.find_all(class_= "views-field views-field-title"): 
    if story_heading.a: 
     print(story_heading.a.text.replace("\n", " ").strip()) 
    else: 
     print(story_heading.contents[0].strip()) 
0

您可以使用CSS選擇器來從最頂部閱讀DIV具體標籤:

from bs4 import BeautifulSoup 

base_url = 'https://www.michigandaily.com/section/opinion' 
r = requests.get(base_url) 
soup = BeautifulSoup(r.text, "html5lib") 
css = "div.pane-most-read-panel-pane-1 a" 
links = [a.text.strip() for a in soup.select(css)] 

,這將給你:

[u'Michigan in Color: Anotha One', u'Migos trio ends 2016 SpringFest with Hill Auditorium concert', u'Migos dabs their way to a seminal moment for Ann Arbor hip hop', u'Best of Ann Arbor 2016', u'Best of Ann Arbor 2016: Full List']