首页 > 解决方案 > Beautifulsoup 响应与查看源代码输出不匹配

问题描述

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

我是python的初学者,所以我的问题听起来很基础。此外,我已经检查了几篇与我的问题相关的文章(BeautifulSoup 返回的 html 与查看源代码不同)但找不到解决方案。

下面是我的代码:

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)

标签: python-3.xbeautifulsoup

解决方案


我有同样的问题,尝试使用另一个解析器,如“lxml”而不是“html5lib”。


推荐阅读