更改 urllib2 的 IP 地址 [英] Changing the IP address for urllib2
本文介绍了更改 urllib2 的 IP 地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试更改我的简单 python 代码连接到我的网站的 IP 地址.
I am trying to change the IP address from which my simple python code connects to my website.
import urllib.request as urllib2
# change of IP address
page = urllib2.urlopen("http://example.com/").read()
是否有可以轻松启用它的 python 库?以便连接到站点的用户显示不同的位置.
Is there a python library that will easily enable it? So that the user who connects to the site displays different locations.
例如,我想使用 IP 地址:118.69.140.108 和端口 53281 从站点抓取本地新闻.
For example, I will want to scrape local news from the site using IP Address: 118.69.140.108 and port 53281.
怎么做,什么库会启用它?
How to do it, what library will enable it?
推荐答案
尝试使用以下代码:
import urllib.request as urllib2
proxy = urllib2.ProxyHandler({"http": "118.69.140.108:53281"})
opener = urllib2.build_opener(proxy)
urllib2.install_opener(opener)
page = urllib2.urlopen("http://example.com/")
或者,您可以使用 requests
库,这会更容易:
Alternatively you can use the requests
library which makes it easier:
import requests
url = "http://example.com/"
page = requests.get(url, proxies={"http":"118.69.140.108:53281"})
希望能帮到你
这篇关于更改 urllib2 的 IP 地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文