爬取新聞列表

2022-08-21 15:57:10 字數 2021 閱讀 3410

獲取單條新聞的#標題#鏈結#時間#**#內容 #點選次數,幷包裝成乙個函式。

獲取乙個新聞列表頁的所有新聞的上述詳情,幷包裝成乙個函式。

獲取所有新聞列表頁的**,呼叫上述函式。

完成所有校園新聞的爬取工作。

完成自己所選其他主題相應資料的爬取工作。

import

requests

import

refrom bs4 import

beautifulsoup

from datetime import

datetime

url='

'res=requests.get(url)

res.encoding='

utf-8

'soup = beautifulsoup(res.text,'

html.parser')

li =soup.select('li'

)def

hits(newsurl):

id=re.search('

_(.*).html

',newsurl).group(1).split('

/')[1]

clickurl='

'.format(id)

click=int(requests.get(clickurl).text.split('

.')[-1].lstrip("

html('

").rstrip("

');"

))

return

click

defgetonepage(listurl):

for news in soup.select('li'

):

if len(news.select('

.news-list-title

'))>0:

title=news.select('

.news-list-title

')[0].text

url=news.select('

a')[0]['

href']

time=news.select('

.news-list-info

')[0].contents[0].text

dt=datetime.strptime(time,'

%y-%m-%d')

source=news.select('

.news-list-info

')[0].contents[1].text

resd=requests.get(url)

resd.encoding='

utf-8

'soupd=beautifulsoup(resd.text,'

html.parser')

detail=soupd.select('

.show-content

')[0].text

click=hits(url)

print

(time,title,dt,source,click)

getonepage(li)

pages=int(soup.select('

.a1')[0].text.rstrip('

條'))//10+1

爬取新聞列表

獲取單條新聞的 標題 鏈結 時間 內容 點選次數,幷包裝成乙個函式。獲取乙個新聞列表頁的所有新聞的上述詳情,幷包裝成乙個函式。獲取所有新聞列表頁的 呼叫上述函式。完成所有新聞的爬取。import requests from bs4 import beautifulsoup res requests....

爬取新聞列表

import requests import re from bs4 import beautifulsoup url res requests.get url res.encoding utf 8 soup beautifulsoup res.text,html.parser n int soup...

爬取網易新聞

爬取網易新聞 在本小節的內容中呢,筆者將會給大家講述如何去過濾我們所不需要的內容。下面的例子,是本人在爬取網易新聞時,遇到的乙個小問題,在定位元素之後,進行列印的時候,出現了部分內容無法正常解析。筆者也是進行了很長時間的嘗試,請教了很多人,才得到的三種方法。我們一起來看。通過寫入檔案的方法過濾元素 ...