Python製作刷網頁流量工具的代碼執行個體

來源:互聯網
上載者:User
本文給大家分享的是一個使用python製作的,可以實現刷網頁流量的小工具,並給大家附上了詳細代碼,有需要的小夥伴可以參考下

準備

必須環境:

Python3

開始

先實現一個簡單的版本,直接上代碼:

import urllib.requestimport urllib.error#建立get方法def get(url): code=urllib.request.urlopen(url).code return codeif __name__ == '__main__':#設定一些基本屬性 url = "http://shua.jb51.net" 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:    code = get(url)   print('訪問:'+str(code))   i = i+1

簡單粗暴,刷的只是 pv,ip 沒變,容易被搜尋引擎發現,下面我們來改進一下

增加代理功能

給 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://shua.jb51.net" #添加代理列表,可以自行去百度擷取 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:   #添加參數   code = get(url,proxies)   print('第'+str(i)+'次代理訪問:'+str(code))   i = i+1

這樣差不多了,不過有個 bug ,如果頁面打不開了或者代理失效了,程式就自動結束了,接下來我們添加異常處理功能

異常處理

定義 mail 方法 ,用來發寄件提醒

def mail(txt): _user = "你的帳號" _pwd = "你的密碼" _to = "收件帳號" msg = MIMEText(txt, 'plain', 'utf-8') #標題 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://shua.jb51.net" 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)

完成!

結語

代碼只有短短的 50 行,程式還可以改進:

例如:代理列表自動擷取,添加介面,擴充下多線程等等

最後給再給大家分享一個其他小夥伴的作品

import urllib2import timeitimport thread import timei = 0mylock = thread.allocate_lock()def test(no,r):  global i  url = 'http://blog.csdn.net'  for j in range(1,r):    req=urllib2.Request(url)     req.add_header("User-Agent","Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0)")     file = urllib2.urlopen(req)    print file.getcode();    mylock.acquire()    i+=1    mylock.release()      print i;  thread.exit_thread()def fast():    thread.start_new_thread(test,(1,50))    thread.start_new_thread(test,(2,50)) fast()time.sleep(15)

經測試,超過兩個線程以上伺服器就會出現503錯誤,所以2個線程剛好

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.