給定一篇新聞的連結newsUrl,擷取該新聞的全部資訊
标題、作者、釋出機關、稽核、來源
釋出時間:轉換成datetime類型
點選:
- newsUrl
- newsId(使用正規表達式re)
- clickUrl(str.format(newsId))
- requests.get(clickUrl)
- newClick(用字元串處理,或正規表達式)
- int()
整個過程包裝成一個簡單清晰的函數。
嘗試去爬取一個你感興趣的網頁。
import requests
from bs4 import BeautifulSoup
from datetime import datetime
import re
if __name__ == '__main__':#"https://www.bilibili.com/read/cv1119650?from=search"
def bzNews(url):
#url不合法會崩掉
res=requests.get(url)
type(res)
res.encoding="utf-8"
soup1=BeautifulSoup(res.text,'html.parser')
#此網頁釋出時間處理
dateTime = soup1.find('meta',{'itemprop':'datePublished'})
type(dateTime)
dateTime = str(dateTime)
#分隔時間格式
DT = re.findall("(\d{4}-\d{1,2}-\d{1,2}\s\d{1,2}:\d{1,2}:\d{1,2})",dateTime)
#作者名稱
au=soup1.select('.author-name')[0].text
#标題
title=soup1.select('.title')[2].text
time = DT
#處理時間
pubtime= datetime.strptime(time[0],'%Y-%m-%d %H:%M:%S')
pubtime = pubtime.strftime('%Y{y}%m{m}%d{d} %U{a}%w %H{h}%M{f}%S{s}%p').format(y='年',m='月',d='日',h='時',f='分',s='秒',a='周 星期')
print(" 作者:",au,'\n',"标題:",title,'\n',"釋出時間:",pubtime,'\n')
def main():
while(1):
url =input("輸入哔哩哔哩的新聞網址:")
bzNews(url)
ending =int(input("輸入0退出"))
if(ending==0):break
main()

另外通過檢視network發現在一個viewinfo的requests對象中有存放點贊,觀看數的json資料,下次再做解析json資料