python-3.x - 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)
解决方案
我有同样的问题,尝试使用另一个解析器,如“lxml”而不是“html5lib”。
推荐阅读
- docker - 如何配置 minikube 以使用 docker 私有注册表
- python - 如何对列表中的两行求和?
- android - 导航图和多个主机活动
- javascript - 使用谷歌地球引擎在列表中查找最接近的日期
- java - 如何在 Android Studio 2019 中捕获图片而不进行预览
- python - 如何合并几个平均字段的csv文件?
- vue.js - 如果满足某些条件,bootstrap-vue 复选框会阻止更改
- laravel - laravel 中的动态前缀路由
- android - 如何在没有构造函数的情况下为对象实例化设置断点?
- javascript - _reactNativeRouterFLux.Actions.EmployeeEdit 不是函数