如何修复爬网(403)

2024-09-26 22:50:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用python3和scrapy。我正在用这个代码获取scrapy shell:

url = "https://www.urban.com.au/projects/melbourne-square-93-119-kavanagh-street-southbank"
headers = {
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36"

}

fet = scrapy.Request(url, headers=headers)
fetch(fet)

它正在显示DEBUG: Crawled (403)

请分享任何想法与返回200响应在scrapy shell。你知道吗


Tags: 代码httpscomurlwwwshellpython3headers
3条回答
headers = {
    'authority': 'www.urban.com.au',
    'cache-control': 'max-age=0',
    'upgrade-insecure-requests': '1',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36',
    'sec-fetch-mode': 'navigate',
    'sec-fetch-user': '?1',
    'dnt': '1',
    'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3',
    'sec-fetch-site': 'none',
    'accept-encoding': 'gzip, deflate, br',
    'accept-language': 'en-US,en;q=0.9',

}

Request('https://www.urban.com.au/projects/melbourne-square-93-119-kavanagh-street-southbank', headers=headers)

你需要模仿和真正的浏览器完全相同的标题

403错误-因为网站显示验证码。
如果解析验证码并提取cookie,它将工作。
使用requests进行调试的示例:

import requests

headers = {
    'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36',
    'cookie': 'your cookie',
}

response = requests.get('https://www.urban.com.au/projects/melbourne-square-93-119-kavanagh-street-southbank', headers=headers)

如果你在浏览器中打开它,它会显示fill captcha以继续。因此,对于来自计算机的高流量,它将要求额外的身份验证。你知道吗

所以你看到的是403

相关问题 更多 >

    热门问题