1. 用requests庫和beautifulsoup庫,爬取校園新聞首頁新聞的標題、鏈結、正文。
import requests2. 分析字串,獲取每篇新聞的發布時間,作者,**,攝影等資訊。from bs4 import beautifulsoup
from datetime import datetime
newsurl = ''
res = requests.get(newsurl)
res.encoding = 'utf-8'
soup = beautifulsoup(res.text,'html.parser')
a=soup.select('li')
for news in a:
if len(news.select('.news-list-title'))>0:
# 獲取標題
b=news.select('.news-list-title')[0].contents[0]
# 獲取鏈結接
c = news.select('a')[0].attrs['href']
# 獲取正文
res1 = requests.get(c)
res1.encoding = 'utf-8'
soup1 = beautifulsoup(res1.text, 'html.parser')
d=soup1.select('#content')[0].text
#發布時間3. 將其中的發布時間由str轉換成datetime型別。info = soup1.select(".show-info")[0].text;
#作者#**
#攝影h = info[info.find('攝影:'):].split()[0].lstrip('攝影:')
# 獲取當前的時間4. 將完整的**及執行結果截圖發布在作業上。now_time = datetime.now();
now_time.year
# 將字串轉化為時間
print(datetime.strptime(e, "%y-%m-%d %h:%m:%s"))
# 將時間轉化為字串
print(now_time.strftime('%y\%m\%d'))
import requestsfrom bs4 import beautifulsoup
from datetime import datetime
newsurl = ''
res = requests.get(newsurl)
res.encoding = 'utf-8'
soup = beautifulsoup(res.text,'html.parser')
a=soup.select('li')
for news in a:
if len(news.select('.news-list-title'))>0:
# 獲取標題
b=news.select('.news-list-title')[0].contents[0]
# 獲取鏈結接
c = news.select('a')[0].attrs['href']
# 獲取正文
res1 = requests.get(c)
res1.encoding = 'utf-8'
soup1 = beautifulsoup(res1.text, 'html.parser')
d=soup1.select('#content')[0].text
# print(b+" "+c+" "+d)
#發布時間
info = soup1.select(".show-info")[0].text;
#作者#**
#攝影h = info[info.find('攝影:'):].split()[0].lstrip('攝影:')
# 獲取當前的時間
now_time = datetime.now();
now_time.year
# 將字串轉化為時間
# print(datetime.strptime(e, "%y-%m-%d %h:%m:%s"))
# 將時間轉化為字串
爬取校園新聞首頁的新聞
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 ...