我正在尝试抓取我所在国家/地区的一个休闲运动队网站,该网站一直阻止我的 Scrapy 尝试。我尝试设置用户代理,但没有成功..一旦我运行 Scrapy,我就会得到 429 未知状态。没有一个200成功。我可以在浏览器中访问该网站,因此我知道我的 IP 没有被阻止。任何帮助将不胜感激。
这是我正在使用的代码:
import scrapy
from scrapy.spiders import Rule, CrawlSpider
from scrapy.linkextractors import LinkExtractor
class QuoteSpider(CrawlSpider):
name = "Quote"
allowed_domains = ["avaldsnes.spoortz.no"]
start_urls = ["https://avaldsnes.spoortz.no/portal/arego/club/7"]
rules = (Rule(LinkExtractor(allow="")),)
custom_settings = {"USER_AGENT": "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)"}
def parse(self, response):
print(response.request.headers)
我尝试抓取网站的链接,但没有一次尝试成功。现在用户代理设置为 Google Bot,但我也尝试过常规的。
在这种情况下,您需要设置标头(而不仅仅是用户代理)。
from scrapy.spiders import Rule, CrawlSpider
from scrapy.linkextractors import LinkExtractor
class QuoteSpider(CrawlSpider):
name = "Quote"
allowed_domains = ["avaldsnes.spoortz.no"]
start_urls = ["https://avaldsnes.spoortz.no/portal/arego/club/7"]
headers = {
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8",
"Accept-Encoding": "gzip, deflate, br",
"Accept-Language": "en-US,en;q=0.5",
"Cache-Control": "no-cache",
"Connection": "keep-alive",
"DNT": "1",
"Host": "avaldsnes.spoortz.no",
"Pragma": "no-cache",
"Sec-Fetch-Dest": "document",
"Sec-Fetch-Mode": "navigate",
"Sec-Fetch-Site": "none",
"Sec-Fetch-User": "?1",
"Upgrade-Insecure-Requests": "1",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36"
}
rules = (Rule(LinkExtractor(allow="")),)
custom_settings = {
'DEFAULT_REQUEST_HEADERS': headers
}
def parse(self, response):
print(response.request.headers)
输出:
[scrapy.core.engine] DEBUG: Crawled (200) <GET https://avaldsnes.spoortz.no/portal/arego/club/7> (referer: None)
...
...
...