-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
34 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# 随机调用代理池中的Ip进行访问 | ||
|
||
import random | ||
import urllib.request | ||
|
||
url = 'http://www.baidu.com/s?wd=ip' | ||
|
||
# 代理ip不稳定,如需要代理ip则要购买,这里展示代理池的用法。 | ||
proxis_pool = [ | ||
{'http': '211.65.197.93:80'}, | ||
{'http': '211.65.197.93:80'} | ||
] | ||
|
||
|
||
proxis = random.choice(proxis_pool) | ||
|
||
headers = { | ||
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36' | ||
} | ||
|
||
# 请求对象的定制 | ||
request = urllib.request.Request(url=url, headers=headers) | ||
|
||
# print(proxis) | ||
|
||
# 用代理必须用下面三个获取响应 | ||
handler = urllib.request.ProxyHandler(proxies=proxis) | ||
opener = urllib.request.build_opener(handler) | ||
response = opener.open(request) | ||
|
||
content = response.read().decode('utf-8') | ||
|
||
with open('dailipool.html', 'w', encoding='utf-8')as fp: | ||
fp.write(content) |