【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。

相关推荐
花酒锄作田2 小时前
使用 pkgutil 实现动态插件系统
python
前端付豪6 小时前
LangChain链 写一篇完美推文?用SequencialChain链接不同的组件
人工智能·python·langchain
曲幽7 小时前
FastAPI实战:打造本地文生图接口,ollama+diffusers让AI绘画更听话
python·fastapi·web·cors·diffusers·lcm·ollama·dreamshaper8·txt2img
老赵全栈实战7 小时前
Pydantic配置管理最佳实践(一)
python
阿尔的代码屋13 小时前
[大模型实战 07] 基于 LlamaIndex ReAct 框架手搓全自动博客监控 Agent
人工智能·python
AI探索者1 天前
LangGraph StateGraph 实战:状态机聊天机器人构建指南
python
AI探索者1 天前
LangGraph 入门:构建带记忆功能的天气查询 Agent
python
FishCoderh1 天前
Python自动化办公实战:批量重命名文件,告别手动操作
python
躺平大鹅1 天前
Python函数入门详解(定义+调用+参数)
python
曲幽1 天前
我用FastAPI接ollama大模型,差点被asyncio整崩溃(附对话窗口实战)
python·fastapi·web·async·httpx·asyncio·ollama