python - 来自不同国家 IP 的请求

标签 python proxy ip

我想在 Playstation 商店中观看来自不同国家/地区的电影内容。但是,PlayStation 商店会根据请求的位置屏蔽 IP,因此无法从美国观看加拿大的电影:

https://store.playstation.com/#!/en-ca/movies/the-house/cid=UV0130-NPVA92773_CN-0000000000236063

有没有办法做类似下面的事情:

url = 'https://store.playstation.com/#!/en-ca/movies/the-house/cid=UV0130-NPVA92773_CN-0000000000236063'
r = requests.get(url, proxy_from = COUNTRY['CA']) # In pseudocode

基本上,这样您就可以指定一个国家,然后从一个 IP 发送请求,该 IP 将被识别为来自该国家。这将如何完成?

最佳答案

如果我没理解错的话,你基本上是在问如何使用代理在 python 中发出 url 请求?

如果是,您可以按如下方式进行:

import urllib2
import urllib
import random

CAproxies = [{"http":"199.201.122.175:3128", "https":"199.201.122.175:3128"},{"http":"192.99.3.129:3128", "https":"192.99.3.129:3128"},{"http":"192.99.246.101:8118", "https":"192.99.246.101:8118"},{"http":"205.205.129.130:443", "https":"205.205.129.130:443"} ]


proxies = urllib2.ProxyHandler(random.choice(CAproxies))

url = 'https://store.playstation.com/#!/en-ca/movies/the-house/cid=UV0130-NPVA92773_CN-0000000000236063'

request = urllib2.Request(url)
request.add_header("User-Agent", "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:25.0) Gecko/20100101 Firefox/25.0")
request.add_header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")

opener = urllib2.build_opener(proxies)
urllib2.install_opener(opener)
r = urllib2.urlopen(request, timeout=15)
html = r.read()

如果您希望服务认为您正在使用浏览器, header 就很好,它们通常可以防御机器人程序。您需要将代理地址替换为您自己的代理,这只是为了说明目的而发明的代理。

可以在此处找到代理列表,例如: http://www.proxy-listen.de/Proxy/Proxyliste.html 如果上面给出的代理不起作用。 如果其中一个代理更适合您的特定位置(假设第二个最适合您,将代理的随机选择更改为仅第二个可能是个好主意。即

random.choice(CAproxies) -> CAproxies[1]

CAproxies[3] 最适合我。 html 的前 250 个字符:

>>> html[0:250]
'<!DOCTYPE html>\n\n<html class="ctry mobvportA rgba">\n  <head>\n    <meta http-equiv="x-ua-compatible" content="IE=edge" />\n    <meta charset="utf-8"/>\n\n    <link rel="dns-prefetch" href="//ajax.googleapis.com">\n    <link rel="dns-prefetch" href="//ssl.'

关于python - 来自不同国家 IP 的请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30292361/

相关文章:

java - 使用 writeReplace 进行 Corba 编码和代理

c# - 重载显式 CAST 运算符

networking - 如何在 C/C++ 中解析存储在 char 缓冲区中的 IP 数据包

java - 登录注册android应用程序无法连接到本地服务器(连接被拒绝)

Python 3.3 + Django 1.6 + 连接器/Python Django 后端 + South

python - $eq 语法无效 Mongo DB

python - 如何使用 OpenCV 删除小的连接对象

python - 未绑定(bind)本地错误: local variable referenced before assignment when using list comprehension in python

javascript - 使用 Node.js 的代理请求:net::ERR_EMPTY_RESPONSE

linux - Banana Pi 静态 IP