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

相关推荐
dxz_tust2 小时前
flow match简单直观理解
开发语言·python·深度学习·扩散模型·流匹配·flow match
写代码的【黑咖啡】2 小时前
Python 中的时间序列特征自动提取工具:tsfresh
开发语言·python
wqwqweee2 小时前
Flutter for OpenHarmony 看书管理记录App实战:个人中心实现
开发语言·javascript·python·flutter·harmonyos
费弗里2 小时前
我的Python环境管理方式,兼顾常用AI工具依赖环境
python·ai
七夜zippoe2 小时前
Python网络编程实战:从TCP/IP到WebSocket的协议演进与核心技术解析
网络·python·websocket·tcp/ip·socket·心跳机制
jj008u2 小时前
Garmin 中国区活动同步到国际区的一个简单实现方案
python·ai编程
GG向前冲2 小时前
【Python 金融量化】线性模型在AAPL股票数据的分析研究
大数据·python·机器学习·ai·金融
程序猿20232 小时前
Java Thread
java·开发语言·python
喵手2 小时前
Python爬虫零基础入门【第九章:实战项目教学·第8节】可观测性:日志规范 + trace_id + 可复现错误包!
爬虫·python·日志规范·python爬虫实战·python爬虫工程化实战·python爬虫零基础入门·可复性错误包