【发布时间】:2021-06-07 20:37:11
【问题描述】:
我正在尝试抓取一个网站,但出现以下错误:
当我显示输出时,我得到了这个:
<body style="margin:0"><p id="cmsg">Please enable JS and disable any ad blocker</p><script>var dd={\'cid\':\'AHrlqAAAAAMAFuvaKabaZaMAVsjd8A==\',\'hsh\':\'05B30BD9055986BD2EE8F5A199D973\',\'t\':\'fe\',\'s\':2089,\'host\':\'geo.captcha-delivery.com\'}</script><script src="https://ct.captcha-delivery.com/c.js"></script>
在网上查了一下,我发现我们需要添加一个标题,否则网站会知道这是一个机器人。我已经添加了它,但仍然是错误。
我正在使用的代码:
from IPython.display import Image
import requests
from bs4 import BeautifulSoup
import requests_cache
import pandas as pd
requests_cache.install_cache("bases_scraping", expire_after=10e5)
url = "https://www.leboncoin.fr/recherche?category=55&text=electric%20bike&locations=r_12"
headers = {"user-agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:81.0) Gecko/20100101 Firefox/81.0"}
response = requests.get(url, headers=headers)
print(response)
print(response.content, "html.parser")
如果有人可以帮助我。
谢谢
【问题讨论】:
-
我们需要查看实际的 URL。你的
headers可能有问题 -
我已经更新了网址
标签: python-3.x web web-scraping beautifulsoup python-requests