python爬虫挂代理
以下是GET的方法,使用的代理接口网站是 http://www.xicidaili.com/nn/
#-*- coding:utf-8 -*-
from bs4 import BeautifulSoup
import requests,chardet,urllib2 ip_list=[]
def get_ip_list(url, headers):
web_data = requests.get(url, headers=headers)
soup = BeautifulSoup(web_data.text, 'lxml')
ips = soup.find_all('tr')
ip_list = []
for i in range(1, len(ips)):
ip_info = ips[i]
tds = ip_info.find_all('td')
ip_list.append('http://' + tds[1].text + ':' + tds[2].text)
return ip_list def get_random_ip(ip_list):
proxies = {'http': ip_list[0]}
return proxies def getip():
global ip_list
url = 'http://www.xicidaili.com/nn/'
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36'}
if not ip_list:
ip_list = get_ip_list(url, headers=headers)
print ip_list
proxies = get_random_ip(ip_list)
return proxies def deleteip():
global ip_list
ip_list.pop(0) def urllink(link): # 网页HTML获取以及编码转换
for i in range(12) :
try:
ip = getip()
print ip
proxy_support = urllib2.ProxyHandler(ip)
opener = urllib2.build_opener(proxy_support)
urllib2.install_opener(opener)
html_1 = urllib2.urlopen(link, timeout=10).read()
break
except Exception,e:
deleteip()
print '错误',i,e
pass
if i==11:
return ''
encoding_dict = chardet.detect(html_1)
web_encoding = encoding_dict['encoding']
if web_encoding == 'utf-8' or web_encoding == 'UTF-8':
html = html_1
else:
html = html_1.decode('gbk', 'ignore').encode('utf-8')
return html print urllink("http://ccdas.ipmph.com/pc/clinicalExam/getClinicalExamDetail?articleId=8165")
python爬虫挂代理的更多相关文章
- Python 爬虫的代理 IP 设置方法汇总
本文转载自:Python 爬虫的代理 IP 设置方法汇总 https://www.makcyun.top/web_scraping_withpython15.html 需要学习的地方:如何在爬虫中使用 ...
- python爬虫构建代理ip池抓取数据库的示例代码
爬虫的小伙伴,肯定经常遇到ip被封的情况,而现在网络上的代理ip免费的已经很难找了,那么现在就用python的requests库从爬取代理ip,创建一个ip代理池,以备使用. 本代码包括ip的爬取,检 ...
- 设置python爬虫IP代理(urllib/requests模块)
urllib模块设置代理 如果我们频繁用一个IP去爬取同一个网站的内容,很可能会被网站封杀IP.其中一种比较常见的方式就是设置代理IP from urllib import request proxy ...
- python爬虫redis-ip代理池搭建几十万的ip数据--可以使用
from bs4 import BeautifulSoupimport requests,os,sys,time,random,redisfrom lxml import etreeconn = re ...
- Python爬虫代理池
爬虫代理IP池 在公司做分布式深网爬虫,搭建了一套稳定的代理池服务,为上千个爬虫提供有效的代理,保证各个爬虫拿到的都是对应网站有效的代理IP,从而保证爬虫快速稳定的运行,当然在公司做的东西不能开源出来 ...
- Python爬虫代理IP池
目录[-] 1.问题 2.代理池设计 3.代码模块 4.安装 5.使用 6.最后 在公司做分布式深网爬虫,搭建了一套稳定的代理池服务,为上千个爬虫提供有效的代理,保证各个爬虫拿到的都是对应网站有效的代 ...
- python爬虫之反爬虫(随机user-agent,获取代理ip,检测代理ip可用性)
python爬虫之反爬虫(随机user-agent,获取代理ip,检测代理ip可用性) 目录 随机User-Agent 获取代理ip 检测代理ip可用性 随机User-Agent fake_usera ...
- Python 爬虫入门(二)—— IP代理使用
上一节,大概讲述了Python 爬虫的编写流程, 从这节开始主要解决如何突破在爬取的过程中限制.比如,IP.JS.验证码等.这节主要讲利用IP代理突破. 1.关于代理 简单的说,代理就是换个身份.网络 ...
- Python爬虫教程-11-proxy代理IP,隐藏地址(猫眼电影)
Python爬虫教程-11-proxy代理IP,隐藏地址(猫眼电影) ProxyHandler处理(代理服务器),使用代理IP,是爬虫的常用手段,通常使用UserAgent 伪装浏览器爬取仍然可能被网 ...
随机推荐
- KNN算法 - 数据挖掘算法(3)
(2017-04-10 银河统计) KNN算法即K Nearest Neighbor算法.这个算法是机器学习里面一个比较经典的.相对比较容易理解的算法.其中的K表示最接近自己的K个数据样本.KNN算法 ...
- ethtools命令详解
1 概述 ethtool 是用于查询及设置网卡参数的命令. 2 命令详解 2.1 命令格式 (1) 语法 ethtool [ -a | -c | -g | -i | -d | -k | -r | -S ...
- Java解决异常之try、catch、finally、throw、throws&log4j记录日志步骤
知识点一.多重catch引发多种类型的异常排列catch 语句的顺序:先子类后父类 发生异常时按顺序逐个匹配只执行第一个与异常类型匹配的catch语句二.异常分类异常分为运行时异常和检测异常运行时异常 ...
- UI自动化(六)js
HTML中的三把利器的JS 又称为JavaScript,看着好像和Java有点联系,实际上他和java半毛钱关系都没有,JavaScript和我们学习的Python.Go.Java.C++等,都是一种 ...
- DataTable与List的相互转换
List转DataTable: public static DataTable ToDataTable<T>(IEnumerable<T> collection) { var ...
- python 获取subprocess进程执行后返回值
test.py #coding=utf- import subprocess compilePopen = subprocess.Popen('gcc haha',shell=True,stderr= ...
- 在pycharm中运行python程序
安装PyCharm 安装过程取决于您的操作系统: 在Windows上安装PyCharm 运行.exe您已下载的文件,并按照PyCharm安装向导的说明进行操作. 在macOS上安装PyCharm 打开 ...
- Angular 学习笔记 (组件沟通的思考)
组件指令间经常需要沟通 我们知道的方式有 input output service inject viewchild contentchild templateRef template variabl ...
- shell脚本中if
[[ $i =~ ^[0-9]+$ ]] && echo 1 的解释 =~ 表示的是匹配 && 是前一个命令为真 才执行后一个命令 [[ ]] 是if判断使用正则表达式 ...
- ES6之Array数组
定义数组 ,]; const arr = new Array(1,2,3,4); const array1 = new Array(); array1[]="test"; 给数组不 ...