python - 使用 BeautifulSoup 进行网页抓取时,我可以接受或忽略 Google 隐私声明吗?
问题描述
从控制台运行以下代码时,我无法查看 Google 新闻页面的 HTML。我看到的 HTML 是 Google 隐私声明(以“在您继续之前”开头的那个)。
from bs4 import BeautifulSoup
import requests
headers = {'User-Agent': 'Mozilla/5.0'}
r = requests.get("https://www.google.com/news", headers=headers)
soup = BeautifulSoup(r.content, 'html.parser')
print(soup.prettify())
有没有办法完全防止隐私通知弹出?
我得到的一个片段:
<title>
Before you continue
</title>
<meta content="initial-scale=1, maximum-scale=5, width=device-width" name="viewport"/>
<link href="//www.google.com/favicon.ico" rel="shortcut icon"/>
</head>
<body>
<div class="signin">
<a class="button" href="https://accounts.google.com/ServiceLogin?hl=en-US&continue=https://news.google.com/topics/CAAqBwgKMKHQ9Qowlc7cAg&gae=cb-">
Sign in
</a>
</div>
<div class="box">
<img alt="Google" height="28" src="//www.gstatic.com/images/branding/googlelogo/1x/googlelogo_color_68x28dp.png" srcset="//www.gstatic.com/images/branding/googlelogo/2x/googlelogo_color_68x28dp.png 2x" width="68"/>
<div class="productLogoContainer">
<img alt="" aria-hidden="true" class="image" height="100%" src="https://www.gstatic.com/ac/cb/scene_cookie_wall_search_v2.svg" width="100%"/>
</div>
解决方案
您可以将CONSENT
cookie 设置为不获取“继续之前”页面:
import requests
from bs4 import BeautifulSoup
headers = {"User-Agent": "Mozilla/5.0"}
cookies = {"CONSENT": "YES+cb.20210720-07-p0.en+FX+410"}
r = requests.get(
"https://www.google.com/news", headers=headers, cookies=cookies
)
soup = BeautifulSoup(r.content, "html.parser")
print(soup.prettify())
推荐阅读
- react-native - 反应原生元素无法识别样式化组件自定义字体
- python - 如何以二进制格式存储来自 NEST 模拟器的记录数据?
- reactjs - 在 React Native 中同时运行任务
- nginx - Nginx 身份验证在根页面(HTTPS)上不起作用
- gstreamer - 在 ubuntu1804 上编译 Gstreamer/orc 0.4.32 以使用 ninja 在 rootfs 中运行 arm 发生未定义的引用异常
- c - 使用链表中的堆栈反转C中的字符串
- rust - 我们如何处理 gtk-rs 中输入框的按钮单击事件?
- .net-core - “处理组件 pubsub 错误:组件 pubsub 的初始化超时超过 5 秒”
- modelsim - 获取 Modelsim 库中已编译模块的列表
- iis - 隐藏/删除堆栈跟踪信息