【问题标题】:Beautifulsoup response does not match with view source code outputBeautifulsoup 响应与查看源代码输出不匹配
【发布时间】:2019-09-12 05:25:28
【问题描述】:

在比较来自代码和 chrome 源代码的响应时。我观察到从 beautifulsoup 返回的响应与页面源代码不匹配。我想获取 class="rc" 并且可以在页面源代码上看到带有“rc”的类,但在打印的响应中找不到它。我也检查了“lxml”和“html.parser”。

我是 python 的初学者,所以我的问题听起来很基础。另外,我已经查看了一些与我的问题相关的文章(BeautifulSoup returning different html than view source),但找不到解决方案。

下面是我的代码:

import sys, requests
import re
import docx
import webbrowser
from bs4 import BeautifulSoup

query = sys.argv
url = "https://google.com/search?q=" + "+".join(query[1:])
print(url)
res = requests.get(url)
# print(res[:1000])

if res.status_code == 200:
    soup = BeautifulSoup(res.text, "html5lib")
    print(type(soup))
    all_select = soup.select("div", {"class": "rc"})
    print("All Select ", all_select)

【问题讨论】:

  • 这经常出现(一个月几次?)。一些内容是动态的,例如包括一些类。 SO上有很多例子可以帮助你。

标签: python-3.x beautifulsoup


【解决方案1】:

我遇到了同样的问题,请尝试使用其他解析器,例如“lxml”而不是“html5lib”。

【讨论】:

    猜你喜欢
    • 2016-07-07
    • 2019-02-24
    • 2018-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-02
    • 1970-01-01
    • 2018-05-10
    相关资源
    最近更新 更多