1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
|
import re import sys import requests
reload(sys) sys.setdefaultencoding('utf-8') requests.packages.urllib3.disable_warnings()
HEADER = {'Connection': 'keep-alive', 'Cache-Control': 'max-age=0', 'Upgrade-Insecure-Requests': '1', 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko)', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8', 'Accept-Encoding': 'gzip, deflate, sdch', 'Accept-Language': 'zh-CN,zh;q=0.8', }
def (func): def decorate(*args, **kwargs): try: return func(*args, **kwargs) except Exception as e: print u"sorry, 抓取出错。错误原因:" print e
return decorate
def getHtmlTree(url, **kwargs): import requests from lxml import etree header = {'Connection': 'keep-alive', 'Cache-Control': 'max-age=0', 'Upgrade-Insecure-Requests': '1', 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko)', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8', 'Accept-Encoding': 'gzip, deflate, sdch', 'Accept-Language': 'zh-CN,zh;q=0.8', } try: html = requests.get(url=url, headers=header, timeout=30).content except: pass return etree.HTML(html)
class GetFreeProxy(object): def __init__(self): pass
@robustCrawl def freeProxyFirst(page=10): """ 抓取快代理IP http://www.kuaidaili.com/ """ url_list = ('http://www.kuaidaili.com/proxylist/{page}/'.format(page=page) for page in range(1, page + 1)) for url in url_list: tree = getHtmlTree(url) proxy_list = tree.xpath('.//div[@id="index_free_list"]//tbody/tr') for proxy in proxy_list: yield ':'.join(proxy.xpath('./td/text()')[0:2])
@robustCrawl def freeProxySecond(proxy_number=100): """ 抓取代理66 http://www.66ip.cn/ """ url = "http://m.66ip.cn/mo.php?sxb=&tqsl={}&port=&export=&ktip=&sxa=&submit=%CC%E1++%C8%A1&textarea=".format( proxy_number) html = requests.get(url, headers=HEADER).content for proxy in re.findall(r'd{1,3}.d{1,3}.d{1,3}.d{1,3}:d{1,5}', html): yield proxy
@robustCrawl def freeProxyThird(): """ 抓取西刺代理 http://api.xicidaili.com/free2016.txt """ url_list = ['http://www.xicidaili.com/nn', 'http://www.xicidaili.com/nt', ] for each_url in url_list: tree = getHtmlTree(each_url) proxy_list = tree.xpath('.//table[@id="ip_list"]//tr') for proxy in proxy_list: yield ':'.join(proxy.xpath('./td/text()')[0:2])
@robustCrawl def freeProxyFourth(page=10): """ 抓取酷伯伯代理 http://www.coobobo.com/free-http-proxy """ url_list = ("http://www.coobobo.com/free-http-proxy/{page}/".format(page=page) for page in range(1, page+1)) for url in url_list: tree = getHtmlTree(url) proxy_list = tree.xpath('.//div[@class="col-md-12"]//tbody/tr') for proxy in proxy_list: prot = proxy.xpath('./td/text()')[1:2][0] ip = proxy.xpath('./td/script/text()')[0] ip = ".".join(re.findall('d+',ip)) yield "{ip}:{prot}".format(ip=ip, prot=prot)
@robustCrawl def freeProxyFifth(page=10): """ 抓取开心代理 http://www.kxdaili.com/dailiip/1/1.html#ip """ url_list = ("http://www.kxdaili.com/dailiip/1/{page}.html#ip".format(page=page) for page in range(1, page+1)) for url in url_list: tree = getHtmlTree(url) proxy_list = tree.xpath('.//table[@class="ui table segment"]//tbody/tr') for proxy in proxy_list: yield ":".join(proxy.xpath('./td/text()')[0:2])
@staticmethod @robustCrawl def freeProxySixth(): """ 抓取ip181 http://www.ip181.com/ """ url = "http://www.ip181.com/" tree = getHtmlTree(url) proxy_list = tree.xpath('.//div[@class="col-md-12"]//tbody/tr') for proxy in proxy_list[1:]: yield ":".join(proxy.xpath('./td/text()')[:2])
if __name__ == '__main__': gg = GetFreeProxy()
for q in gg.freeProxySixth(): print q
|
近期评论