python獲取代理IP的實例分享-創(chuàng)新互聯(lián)

平時當我們需要爬取一些我們需要的數(shù)據(jù)時,總是有些網(wǎng)站禁止同一IP重復訪問,這時候我們就應該使用代理IP,每次訪問前偽裝自己,讓“敵人”無法察覺。

成都創(chuàng)新互聯(lián)公司網(wǎng)站建設公司一直秉承“誠信做人,踏實做事”的原則,不欺瞞客戶,是我們最起碼的底線! 以服務為基礎,以質(zhì)量求生存,以技術求發(fā)展,成交一個客戶多一個朋友!專注中小微企業(yè)官網(wǎng)定制,成都網(wǎng)站設計、網(wǎng)站制作、外貿(mào)營銷網(wǎng)站建設,塑造企業(yè)網(wǎng)絡形象打造互聯(lián)網(wǎng)企業(yè)效應。

oooooooooooooooOK,讓我們愉快的開始吧!

這個是獲取代理ip的文件,我將它們模塊化,分為三個函數(shù)

注:文中會有些英文注釋,是為了寫代碼方便,畢竟英文一兩個單詞就ok了

#!/usr/bin/python
#-*- coding:utf-8 -*-
"""
author:dasuda
"""
import urllib2
import re
import socket
import threading
findIP = [] #獲取的原始IP數(shù)據(jù)
IP_data = [] #拼接端口后的IP數(shù)據(jù)
IP_data_checked = [] #檢查可用性后的IP數(shù)據(jù)
findPORT = [] #IP對應的端口
available_table = [] #可用IP的索引
def getIP(url_target):
 patternIP = re.compile(r'(?<=<td>)[\d]{1,3}\.[\d]{1,3}\.[\d]{1,3}\.[\d]{1,3}')
 patternPORT = re.compile(r'(?<=<td>)[\d]{2,5}(?=</td>)')
 print "now,start to refresh proxy IP..."
 for page in range(1,4):
  url = 'http://www.xicidaili.com/nn/'+str(page)
  headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64)"}
  request = urllib2.Request(url=url, headers=headers)
  response = urllib2.urlopen(request)
  content = response.read()
  findIP = re.findall(patternIP,str(content))
  findPORT = re.findall(patternPORT,str(content))
  #assemble the ip and port
  for i in range(len(findIP)):
   findIP[i] = findIP[i] + ":" + findPORT[i]
   IP_data.extend(findIP)
  print('get page', page)
 print "refresh done!!!"
 #use multithreading
 mul_thread_check(url_target)
 return IP_data_checked
def check_one(url_check,i):
 #get lock
 lock = threading.Lock()
 #setting timeout
 socket.setdefaulttimeout(8)
 try:
  ppp = {"http":IP_data[i]}
  proxy_support = urllib2.ProxyHandler(ppp)
  openercheck = urllib2.build_opener(proxy_support)
  urllib2.install_opener(openercheck)
  request = urllib2.Request(url_check)
  request.add_header('User-Agent',"Mozilla/5.0 (Windows NT 10.0; WOW64)")
  html = urllib2.urlopen(request).read()
  lock.acquire()
  print(IP_data[i],'is OK')
  #get available ip index
  available_table.append(i)
  lock.release()
 except Exception as e:
  lock.acquire()
  print('error')
  lock.release()
def mul_thread_check(url_mul_check):
 threads = []
 for i in range(len(IP_data)):
  #creat thread...
  thread = threading.Thread(target=check_one, args=[url_mul_check,i,])
  threads.append(thread)
  thread.start()
  print "new thread start",i
 for thread in threads:
  thread.join()
 #get the IP_data_checked[]
 for error_cnt in range(len(available_table)):
  aseemble_ip = {'http': IP_data[available_table[error_cnt]]}
  IP_data_checked.append(aseemble_ip)
 print "available proxy ip:",len(available_table)

網(wǎng)頁標題:python獲取代理IP的實例分享-創(chuàng)新互聯(lián)
轉(zhuǎn)載源于:http://muchs.cn/article28/djeijp.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供全網(wǎng)營銷推廣做網(wǎng)站、標簽優(yōu)化、營銷型網(wǎng)站建設、網(wǎng)站排名、Google

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

搜索引擎優(yōu)化