Python:请求 - 抓取工具不再工作 - cookie 问题(数据隐私同意)?
Python: requests - scraper does not work anymore - issue with cookie (data privacy consent)?
Whosebug 社区,
两个月前,我在 python3 中创建了一个抓取工具,目的是从 https://www.autoscout24.com 抓取汽车广告。
抓取工具使用以下 search URL 获取所有可用汽车广告的列表,然后遍历详细信息页面。
自 6 月 26 日起,由于新的 cookie 隐私许可,爬虫无法再从网站上获取汽车广告,至少我是这么认为的。
当遍历所有 <a>
标签以获取汽车广告的详细信息页面(以 /offers/ 开头)时,没有显示任何结果。过去,打印汽车广告详细信息页面的链接。
请在下面找到我获取汽车广告详细信息页面的代码。
from bs4 import BeautifulSoup, SoupStrainer
from requests_html import HTMLSession
import re
url = 'https://www.autoscout24.com/lst/porsche/911-series-(all)?sort=age&desc=1&ustate=N%2CU&size=20&page=1&pricefrom=0&priceto=100000&fregfrom=1990&fregto=1995&cy=D&atype=C&'
session = HTMLSession()
r = session.get(url)
only_a_tags = SoupStrainer("a")
soup = BeautifulSoup(r.content,'lxml', parse_only=only_a_tags)
for link in soup.find_all("a"):
if r"/offers/" in str(link.get("href")):
print (link.get("href"))
我已经尝试设置“cookies = {'cookieConsent': '1'}”并将其添加到获取请求中,但仍然不起作用。
四只眼睛比两只眼睛看到更多,如果你们中的一些人能花点时间帮我解决这个问题,我将不胜感激。
非常感谢您的支持,祝您今天愉快。
试试这个:
from bs4 import BeautifulSoup, SoupStrainer
from requests_html import HTMLSession
import re
url = 'https://www.autoscout24.com/lst/porsche/911-series-(all)?sort=age&desc=1&ustate=N%2CU&size=20&page=1&pricefrom=0&priceto=100000&fregfrom=1990&fregto=1995&cy=D&atype=C&'
with HTMLSession() as session:
r = session.get(url)
r.html.render()
only_a_tags = SoupStrainer("a")
soup = BeautifulSoup(r.html.html,'lxml', parse_only=only_a_tags)
for link in soup.find_all("a"):
if r"/offers/" in str(link.get("href")):
print (link.get("href"))
这样做是在页面上呈现 JS,这似乎是问题所在,因为您在不呈现 JS 的情况下进行抓取,并且所有报价都由 JS 提取。
希望这对您有所帮助:)
编辑:有时您可能不得不 运行 它不止一次,我不确定为什么,但它似乎大部分时间都可以在我的机器上运行
非常感谢您的快速回复。
由于 chromium 问题(例如 import pyppdf.patch_pyppeteer)进行了一些调试后,我终于让它与你的修复一起工作。效果很好!
非常感谢:-)
Whosebug 社区,
两个月前,我在 python3 中创建了一个抓取工具,目的是从 https://www.autoscout24.com 抓取汽车广告。 抓取工具使用以下 search URL 获取所有可用汽车广告的列表,然后遍历详细信息页面。
自 6 月 26 日起,由于新的 cookie 隐私许可,爬虫无法再从网站上获取汽车广告,至少我是这么认为的。
当遍历所有 <a>
标签以获取汽车广告的详细信息页面(以 /offers/ 开头)时,没有显示任何结果。过去,打印汽车广告详细信息页面的链接。
请在下面找到我获取汽车广告详细信息页面的代码。
from bs4 import BeautifulSoup, SoupStrainer
from requests_html import HTMLSession
import re
url = 'https://www.autoscout24.com/lst/porsche/911-series-(all)?sort=age&desc=1&ustate=N%2CU&size=20&page=1&pricefrom=0&priceto=100000&fregfrom=1990&fregto=1995&cy=D&atype=C&'
session = HTMLSession()
r = session.get(url)
only_a_tags = SoupStrainer("a")
soup = BeautifulSoup(r.content,'lxml', parse_only=only_a_tags)
for link in soup.find_all("a"):
if r"/offers/" in str(link.get("href")):
print (link.get("href"))
我已经尝试设置“cookies = {'cookieConsent': '1'}”并将其添加到获取请求中,但仍然不起作用。
四只眼睛比两只眼睛看到更多,如果你们中的一些人能花点时间帮我解决这个问题,我将不胜感激。
非常感谢您的支持,祝您今天愉快。
试试这个:
from bs4 import BeautifulSoup, SoupStrainer
from requests_html import HTMLSession
import re
url = 'https://www.autoscout24.com/lst/porsche/911-series-(all)?sort=age&desc=1&ustate=N%2CU&size=20&page=1&pricefrom=0&priceto=100000&fregfrom=1990&fregto=1995&cy=D&atype=C&'
with HTMLSession() as session:
r = session.get(url)
r.html.render()
only_a_tags = SoupStrainer("a")
soup = BeautifulSoup(r.html.html,'lxml', parse_only=only_a_tags)
for link in soup.find_all("a"):
if r"/offers/" in str(link.get("href")):
print (link.get("href"))
这样做是在页面上呈现 JS,这似乎是问题所在,因为您在不呈现 JS 的情况下进行抓取,并且所有报价都由 JS 提取。
希望这对您有所帮助:)
编辑:有时您可能不得不 运行 它不止一次,我不确定为什么,但它似乎大部分时间都可以在我的机器上运行
非常感谢您的快速回复。 由于 chromium 问题(例如 import pyppdf.patch_pyppeteer)进行了一些调试后,我终于让它与你的修复一起工作。效果很好!
非常感谢:-)