【BUG】【Python】【爬虫】爬取加载中的数据

示例网页链接:https://movie.douban.com/subject/36907263/

BUG

浏览器开发者模式可以看到所需信息有对应的HTML显式结构

但代码爬取时发现结构被hidden,需要二次加载

python 复制代码
import requests

url = 'https://movie.douban.com/subject/36907263/'
headers = {'User-Agent': 'Mozilla/5.0'}
response = requests.get(url, headers=headers)
print(response.text)

即便使用selenium将修改type属性,让其不隐藏,再取出被隐藏的数据,也不行

python 复制代码
from selenium import webdriver

driver = webdriver.Chrome()
driver.get('https://movie.douban.com/subject/36907263/')
js="document.getElementById('red').type='text';"
driver.execute_script(js)
content = driver.find_element(by='id',value='red').text
print(content)
driver.quit()



其实我觉得这个逻辑是对的,但结果非预期,求助是不是哪块代码没写对? T^T 想打印下修改后的HTML也没打印出来 o(╥﹏╥)o

DEBUG

Selenium 的核心功能是模拟用户在浏览器中的操作,所以只需要增加等待时间,等待页面全部加载出来即可。加载后的页面就是我们在浏览器开发者模式下看到的全部HTML结构。

python 复制代码
from selenium import webdriver
import time

driver = webdriver.Chrome()
driver.get('https://movie.douban.com/subject/36907263/')
time.sleep(5)  # 等待页面加载
content = driver.find_element(by='id',value='info').text
print(content)
driver.quit()

注意加载后要找的就是加载后的数据,即id=info,不再是之前的id=red。

相关推荐
Smartdaili China7 小时前
OpenClaw赋能AI智能体:实时联网与网页抓取
人工智能·爬虫·ai·爬取·openclaw·open claw
EntyIU8 小时前
mineru从安装部署到测试使用完整指南
python·ocr
安替-AnTi8 小时前
厚朴 APK 搜索接口分析
python·apk·解析·taobao
山川湖海9 小时前
AI时代快速学编程语言的陷阱(以Python为例)
大数据·人工智能·python
H Journey9 小时前
Supervisor 进程管理工具介绍
python·supervisor·linux 运维
春日见9 小时前
5分钟入门强化学习之动态规划算法与实现
大数据·人工智能·python·算法·机器学习·计算机视觉
DeniuHe10 小时前
sklearn 中所有交叉验证数据集划分方式完整总结
人工智能·python·sklearn
DeniuHe10 小时前
sklearn中不同交叉验证方法的场景适配
人工智能·python·sklearn
隐于花海,等待花开10 小时前
16.Python 常用第三方库概览 深度解析
python
我材不敲代码10 小时前
Python 函数核心:位置参数与关键字参数详解
java·前端·python