python - 如何在尝试抓取网站时更改我的位置?
问题描述
如果访问者不在土耳其,我正在开发一个不显示任何产品的网站。该网站是家乐福。当我尝试用我的电脑进行刮擦时,这没关系,因为我在土耳其的位置。我的服务器位于德国,由于位置原因,蜘蛛无法在服务器上运行。我已经尝试了如下方法:
我尝试用 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: ******
除了代理,你有什么建议吗?
解决方案
我向我的蜘蛛添加了一个元标记,它解决了我的问题。
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):
...
推荐阅读
- sql-server - 无法从 MS SQL Server 2016 写入目录 C:\inetpub
- xaml - Avalonia 中的 Resource.xaml
- python - 更改模型后重新调整 QTableView
- vba - 如何使用宏向 Outlook 添加水平规则?
- php - AppEngine BigQuery PHP 库在运行时不隐含?
- android - Android 如何复用同一个数据绑定类?
- neural-network - 用于对处于两种不同状态的连续数据进行分类的神经网络类型
- c# - WPF DevExpress GridControl MVVM 如何将在代码中创建的网格布局字符串值返回给 viewmodel
- angular - Angular 8 中的 Response.json() 在哪里?
- docker - 如何在 docker compose 中创建 docker 卷