小福利阱扬,教你用python爬蟲獲取騰訊新聞
在嘗試了多個模塊bs4,lxml,re,json,多種方法之后伸辟,終于找到了方法麻惶,功夫不負(fù)有心人,付出必有回報信夫。
import requests, re, lxml
from lxml import etree
from bs4 import BeautifulSoup
import json
import pandas as pd
headers = {
'Referer': 'https://news.qq.com',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; rv:46.0) Gecko/20100101 Firefox/46.0',
}
url = 'https://i.news.qq.com/trpc.qqnews_web.kv_srv.kv_srv_http_proxy/list?sub_srv_id=24hours&srv_id=pc&offset=0&limit=20&strategy=1&ext={"pool":["top"],"is_filter":7,"check_type":true}'
res = requests.get(url, headers=headers)
res1=res.json()
res2=res1['data']
datas=res2['list']
datas_list = []
for i in datas:
datas_dict = {}
datas_dict['標(biāo)題']=i['title']
datas_dict['網(wǎng)址'] = i['url']
datas_dict['圖片網(wǎng)址'] = i['thumb_nail']
datas_list.append(datas_dict)
df=pd.DataFrame(datas_list)
print(df)
獲得數(shù)據(jù)截圖窃蹋,還可以存到excel文件里面
[圖片上傳失敗...(image-6c2235-1648970395328)]