我使用Python3
和urllib3
来爬取和下载网站。我抓取了4000个不同域名的列表,其中大约5个我回来了HttpErrorCode
- 403 - 'Forbidden'
。Python - urllib3在爬取网站时收到403'禁止'
在我的浏览器中,网站确实存在并且正确响应。可能这些网站怀疑我是一个爬虫,并且禁止我获取数据。
这是我的代码:网站
from urllib3 import PoolManager, util, Retry
import certifi as certifi
from urllib3.exceptions import MaxRetryError
manager = PoolManager(cert_reqs='CERT_REQUIRED',
ca_certs=certifi.where(),
num_pools=15,
maxsize=6,
timeout=40.0,
retries=Retry(connect=2, read=2, redirect=10))
url_to_download = "https://www.uvision.co.il/"
headers = util.make_headers(accept_encoding='gzip, deflate',
keep_alive=True,
user_agent="Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:47.0) Gecko/20100101 Firefox/47.0")
headers['Accept-Language'] = "en-US,en;q=0.5"
headers['Connection'] = 'keep-alive'
headers['Accept'] = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
try:
response = manager.request('GET',
url_to_download,
preload_content=False,
headers=headers)
except MaxRetryError as ex:
raise FailedToDownload()
例已经拒绝了我: https://www.uvision.co.il/和http://www.medyummesut.net/。
另一个网站,不工作,抛出MaxRetryError
是:
我也试着使用Firefox的使用完全相同的头文件,它也不能工作。我在这里做错了什么?
添加util导入和PoolManager实例。 –