爬取校園新聞首頁的新聞

2022-06-04 18:21:07 字數 2304 閱讀 2361

1. 用requests庫和beautifulsoup庫,爬取校園新聞首頁新聞的標題、鏈結、正文。

import

requests

from bs4 import

beautifulsoup

url = '

'res =requests.get(url)

res.encoding = '

utf-8

'soup = beautifulsoup(res.text, '

html.parser')

news = soup.select('li'

)for new in

news:

if len(new.select('

.news-list-title

')) >0:

title = new.select('

.news-list-title

')[0].text

href = new.select('

a')[0].attrs['

href']

text = new.select('

.news-list-description

')[0].text

print

(title)

print

(href)

print(text+'

\n')

只展示部分

2. 分析字串,獲取每篇新聞的發布時間,作者,**,攝影等資訊。

3. 將其中的發布時間由str轉換成datetime型別。

4. 將完整的**及執行結果截圖發布在作業上。

爬取校園新聞首頁的新聞

1.用requests庫和beautifulsoup庫,爬取校園新聞首頁新聞的標題 鏈結 正文 show info。2.分析info字串,獲取每篇新聞的發布時間,作者,攝影等資訊。import requests newsurl res requests.get newsurl 返回response物...

爬取校園新聞首頁的新聞

1.用requests庫和beautifulsoup庫,爬取校園新聞首頁新聞的標題 鏈結 正文 show info。import requests from bs4 import beautifulsoup newsurl res requests.get newsurl res.encoding ...

爬取校園新聞首頁的新聞

1.用requests庫和beautifulsoup庫,爬取校園新聞首頁新聞的標題 鏈結 正文 show info。2.分析info字串,獲取每篇新聞的發布時間,作者,攝影等資訊。import requests from bs4 import beautifulsoup from datetime ...