我如何在尝试抓取网站时更改位置?

2024-04-25 08:56:59 发布

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

我在一个网站上工作,如果访问者不在土耳其,该网站不会展示任何产品。该网站是Carrefoursa。当我试着用我的电脑刮东西的时候,这是可以的,因为我在土耳其。我的服务器位于德国,由于位置原因,spider无法在服务器上工作。我已经尝试了以下方法:

我尝试通过请求发送它

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: ******

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


Tags: nameself服务器comtrueparse网站request
1条回答
网友
1楼 · 发布于 2024-04-25 08:56:59

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

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):
             ...

相关问题 更多 >

    热门问题