【问题标题】:Missing html in response using python requests and beautifulsoup4使用python请求和beautifulsoup4响应缺少html
【发布时间】:2016-08-29 12:55:18
【问题描述】:

当我在浏览器中查看页面源代码时,我所追求的 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。

【问题讨论】:

  • 试试soup.find_all('a', 'button')。顺便说一句,您在 param 类中有错字:soup.find_all('a', class='button')

标签: python html beautifulsoup python-requests


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    试试这个

    soup = BeautifulSoup(r.text, "html.parser")
    for link in soup.findAll('a'):
            print link.get('onclick')
    

    【讨论】:

    • 谢谢,但我尝试了您建议的解析器,但没有成功。如果我查看请求响应中的文本,我仍然看不到那里的 html。是否有任何原因会在我的浏览器中呈现它而不是在 Python 请求中?
    • 我没有在源代码中找到您的 html 部分,并在dota2lounge.com/match?m=13362 url 上尝试此代码它找到 2 onclick selectTeam($(this), 'a') FUNCTIONS there.
    猜你喜欢
    • 1970-01-01
    • 2021-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多