個人博客:凌霄的博客
準(zhǔn)備
-
必須環(huán)境:
- Python3
開始
- 先實現(xiàn)一個簡單的版本羔挡,直接上代碼:
import urllib.request
import urllib.error
#創(chuàng)建get方法
def get(url):
code=urllib.request.urlopen(url).code
return code
if __name__ == '__main__':
#設(shè)置一些基本屬性
url = "http://lx.nextdev.cn"
user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.63 Safari/537.36"
headers = {'User-Agent':user_agent}
req = urllib.request.Request(url, headers=headers)
#記錄次數(shù)
i = 1
while 1:
code = get(url)
print('訪問:'+str(code))
i = i+1
簡單粗暴,刷的只是 pv
,ip
沒變派撕,容易被搜索引擎發(fā)現(xiàn)婉弹,下面我們來改進一下
增加代理功能
- 給
get
方法添加以下代碼:
random_proxy = random.choice(proxies)
proxy_support = urllib.request.ProxyHandler({"http":random_proxy})
opener = urllib.request.build_opener(proxy_support)
urllib.request.install_opener(opener)
- 修改一下主方法:
if __name__ == '__main__':
url = "http://lx.nextdev.cn"
#添加代理列表,可以自行去百度獲取
proxies = ["124.88.67.22:80","124.88.67.82:80","124.88.67.81:80","124.88.67.31:80","124.88.67.19:80","58.23.16.240:80"]
user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.63 Safari/537.36"
headers = {'User-Agent':user_agent}
req = urllib.request.Request(url, headers=headers)
i = 1
while 1:
#添加參數(shù)
code = get(url,proxies)
print('第'+str(i)+'次代理訪問:'+str(code))
i = i+1
這樣差不多了终吼,不過有個 bug
镀赌,如果頁面打不開了或者代理失效了,程序就自動結(jié)束了际跪,接下來我們添加異常處理功能
異常處理
- 定義
mail
方法 商佛,用來發(fā)郵件提醒
def mail(txt):
_user = "你的賬號"
_pwd = "你的密碼"
_to = "收件賬號"
msg = MIMEText(txt, 'plain', 'utf-8')
#標(biāo)題
msg["Subject"] = "代理失效!"
msg["From"] = _user
msg["To"] = _to
try:
#這里我用的qq郵箱
s = smtplib.SMTP_SSL("smtp.qq.com", 465)
s.login(_user, _pwd)
s.sendmail(_user, _to, msg.as_string())
s.quit()
print("Success!")
except smtplib.SMTPException as e:
print("Falied,%s" % e)
- 然后我們修改一下主方法:
if __name__ == '__main__':
url = "http://lx.nextdev.cn"
proxies = ["124.88.67.22:80","124.88.67.82:80","124.88.67.81:80","124.88.67.31:80","124.88.67.19:80","58.23.16.240:80"]
user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.63 Safari/537.36"
headers = {'User-Agent':user_agent}
req = urllib.request.Request(url, headers=headers)
i = 1
while 1:
try:
code = get(url,proxies)
print('第'+str(i)+'次代理訪問:'+str(code))
i = i+1
except urllib.error.HTTPError as e:
print(e.code)
#添加mail方法
mail(e.code)
except urllib.error.URLError as err:
print(err.reason)
#添加mail方法
mail(err.reason)
完成姆打!
結(jié)語
代碼只有短短的 50 行良姆,程序還可以改進:
例如:代理列表自動獲取,添加界面
歡迎評論