python - 如何使用 Python 请求更改我的当前位置?
问题描述
我正在尝试抓取亚马逊新加坡的卖家数量。为此,我使用了连接到新加坡服务器的 VPN 服务。但是我不想再使用它了,因为它的免费使用时间已过。我不想使用 VPN,而是想使用requests
库更改我的位置。
这是我的代码:
# Set asin, link and headers
asin = XXXXXXXXXX
link = 'https://www.amazon.sg/dp/' + str(asin)
# Headers
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36'}
# Set requests object
res = requests.get(link, params = {"format": "json"}, headers = headers)
# Create soup object
soup_data = BeautifulSoup(res.text, 'html.parser')
print(soup_data)
当我运行这段代码时,亚马逊看到我在土耳其(这是我当前的位置)。而且我看不到有多少人在销售该产品,因为亚马逊在土耳其看到了我。我想将我的位置更改为新加坡。如何使用requests
库或更改来做到这一点headers
?
亚马逊有GET
更改位置的参数吗?如:
www.amazon.sg/dp/ASIN_NUMBER?LOCATION=SINGAPORE
希望大家明白我的意思。
解决方案
亚马逊可能正在使用您的 IP 来确定您的位置。您可以尝试使用一些公开可用的代理:
def main():
import requests
url = "https://d.pub.network/init"
for address in ("http://45.77.151.33:8080", "http://136.244.83.131:8080"):
proxies = {
"https": address
}
response = requests.get(url, proxies=proxies)
content = response.json()
city = content["location"]["cityName"]
country = content["location"]["countryCode"]
print("I think you are in {}, {}".format(city, country))
return 0
if __name__ == "__main__":
import sys
sys.exit(main())
输出:
I think you are in Piscataway, US
I think you are in Frankfurt am Main, DE
>>>
推荐阅读
- react-hooks - 为什么 React Flow 拒绝这种方法?
- r - Get activation functions (and hyperparamters) of trained model keras/tensorflow in R Studio
- java - Java:将一个列表添加到另一个列表而不复制引用
- python - 在python中,使用函数的参数重命名变量
- django - django 2.1 HTML表单提交到数据库错误
- python - TypeError:只能将元组(不是“浮动”)连接到元组 cousera deeplearnig.ai
- c - 我已从 txt 文件中读取数据并存储在结构数组中,并希望按名称字段对数据进行排序
- c# - 将条件映射为真或忽略应用
- java - 如何在空手道 dsl 中使用肥皂响应 (xml) 验证 xml 架构 (XSD)
- android - 远程配置值获取不一致