gpt4 book ai didi

python - 使用 python 请求和 beautifulsoup4 响应时缺少 html

转载 作者:可可西里 更新时间:2023-11-01 13:49:16 25 4
gpt4 key购买 nike

当我在浏览器中查看页面源代码时,我之后的 html 出现在那里。但是,当我使用 python 请求发出请求时,html 不会出现。

我要抓取的网址是 http://dota2lounge.com/match?m=13362 ,而我在页面中所追求的具体 html 是。

<div class="full">
<a class="button" onclick="ChoseEvent(13362,'Whole Match',false)">Match</a>
<a class="button" onclick="ChoseEvent(13392,'1st Game','1462327200')">1st Game</a>
<a class="button" onclick="ChoseEvent(13424,'2nd Game','1462327200')">2nd Game</a>
<br><div id="toma" class="full" style="background: #444;line-height: 2.5rem;border: 1px solid #333;text-align: center;">Whole Match</div>
</div>

我想获取按钮的“onclick”值。到目前为止,我已经尝试过:

r = requests.get('http://dota2lounge.com/match?m=13268')
soup = bs(r.content, 'lxml')
buttons = soup.find_all('a', class_='button')

这是行不通的。

r.content

似乎也没有显示 html。

最佳答案

看起来你想要的元素是由 javascript 添加的,当你在 python 中发出请求时,它没有运行。查看this question .

如果您只是抓取一次(即您只是想要数据,而不是尝试构建一个机器人来为您玩游戏),最快的选择通常是创建一个仅包含以下内容的 .htm 文件指向您要抓取的每个页面的链接(将每个链接放在 <a> 标签中,您甚至不需要文本)。然后你可以使用像downthemall这样的工具在 firefox 中以正确的格式保存每个页面的本地副本。

关于python - 使用 python 请求和 beautifulsoup4 响应时缺少 html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37021747/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com