python爬虫JS逆向爬取https://www.iqiyi.com/u/1290995902/videos前10页视频的 视频标题、URL、当前热度、评论数
时间: 2023-12-21 14:03:26 浏览: 97
iqi/qnsm+dpdk安装
5星 · 资源好评率100%
import requests
import execjs
from bs4 import BeautifulSoup
# 构造请求头
headers = {
'referer': 'https://www.iqiyi.com/u/1290995902/videos',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
}
# 构造参数
params = {
'uid': '1290995902',
'type': 'video',
'mode': '11',
'page': '1',
'size': '30',
'status': '0'
}
# 构造加密参数
with open('iqiyi.js', 'r', encoding='utf-8') as f:
js_code = f.read()
ctx = execjs.compile(js_code)
enc_param = ctx.call('get_param', params)
# 发送请求
url = 'https://pcw-api.iqiyi.com/search/recommend/list'
response = requests.get(url, headers=headers, params=enc_param)
# 解析响应
soup = BeautifulSoup(response.content, 'html.parser')
video_list = soup.select('div.wrapper-list > div.wrapper-piclist > ul > li')
for video in video_list:
title = video.select_one('a')['title']
url = 'https:' + video.select_one('a')['href']
hot = video.select_one('span.num').text
comment = video.select_one('a.comment-num').text
print(title, url, hot, comment)
阅读全文