首页 > 解决方案 > 如何在尝试抓取网站时更改我的位置?

问题描述

如果访问者不在土耳其,我正在开发一个不显示任何产品的网站。该网站是家乐福。当我尝试用我的电脑进行刮擦时,这没关系,因为我在土耳其的位置。我的服务器位于德国,由于位置原因,蜘蛛无法在服务器上运行。我已经尝试了如下方法:

我尝试用 Request 发送它

class CarrefoursaSpider(scrapy.Spider):
    name = 'carrefoursa'
    allowed_domains = ['www.carrefoursa.com']
    start_urls = ['https://www.carrefoursa.com/meyve/c/1015']
    custom_settings = {
        "LOG_FILE":"scrapy_logs/"+name+".log",
        "ROBOTSTXT_OBEY":False,
        "USER_AGENTS":None,
        "COOKIES_ENABLED":True,
        "COOKIES_DEBUG" : True
        }
    def parse(self,reponse):
        request = scrapy.Request(
                reponse.url, callback=self.parse_product,cookies={'Content-Language':'tr','currency': 'TRY', 'country': 'TR','lang': 'tr'}, dont_filter=True)
        yield request
        
    def parse_product(self, response):
             ...

我尝试将网站与另一个国家的 VPN 连接,但出现以下错误。

The requested URL was rejected. Please consult with your administrator.

Your support ID is: ******

除了代理,你有什么建议吗?

标签: pythonweb-scrapingscrapyweb-crawler

解决方案


我向我的蜘蛛添加了一个元标记,它解决了我的问题。

class CarrefoursaSpider(scrapy.Spider):
    name = 'carrefoursa'
    allowed_domains = ['www.carrefoursa.com']
    start_urls = ['https://www.carrefoursa.com/meyve/c/1015']
    meta={'proxy': 'xxx.xxx.xxx.xx:xxxx'},
    custom_settings = {
        "LOG_FILE":"scrapy_logs/"+name+".log",
        "ROBOTSTXT_OBEY":False,
        "USER_AGENTS":None,
        "COOKIES_ENABLED":True,
        "COOKIES_DEBUG" : True
        }
    def parse(self,reponse):
        request = scrapy.Request(
                reponse.url, callback=self.parse_product,cookies={'Content-Language':'tr','currency': 'TRY', 'country': 'TR','lang': 'tr'}, dont_filter=True)
        yield request
        
    def parse_product(self, response):
             ...

推荐阅读