使用代理的爬虫小程序
2017-12-06 14:39
197 查看
使用使用ip117.135.250.134端口80作为代理服务器,爬取了百度首页的代码。
import urllib.request import os import sys import re def testArgument(url): TP=TestProxy(url) def tipUse(): print('改程序只能输入一个参数,这个参数必须是可用的proxy') print('usage:python test Urllib2WithProxy.py http//1.2.3.4:5') print('usage:python test Urllib2WithProxy.py https//1.2.3.4:5') class TestProxy(object): def __init__(self,proxy): self.proxy = proxy self.checkProxyFormat(self.proxy) self.url = 'http://www.baidu.com' self.timeout=5 self.flagWord='百度' self.useProxy(self.proxy) def checkProxyFormat(self,proxy): try: proxyMatch = re.compile('http[s]?://[\d]{1,3}\.{\d}{1,3}\.[\d]{1,3}:[\d]{1,5}$') proxyMatch.match(proxy) except AttributeError: tipUse() exit() flag = 1 proxy = proxy.replace('//','') try: protocol = proxy.split(':')[0] ip = proxy.split(':')[1] port = proxy.split(':')[2] except IndexError: print('下标出界') tipUse() exit() flag = flag and len(proxy.split(':')) and len(ip.split('.')) flag = ip.split('.')[0] in map(str,range(1,256)) and flag flag = ip.split('.')[1] in map(str,range(256)) and flag flag = ip.split('.')[2] in map(str,range(256)) and flag flag = ip.split('.')[3] in map(str,range(1,255)) and flag flag = protocol in ['http','https'] and flag flag = port in map(str,range(1,65535)) and flag if flag: print('输入的http代理服务器符合标准') else: tipUse() exit() def useProxy(self,proxy): protocol = proxy.split('//')[0].replace(':','') ip = proxy.split('//')[1] opener = urllib.request.build_opener(urllib.request.ProxyHandler({protocol:ip})) urllib.request.install_opener(opener) try: response = urllib.request.urlopen(self.url,timeout=self.timeout) except: print('连接错误,退出程序') exit() data = response.read() data = data.decode('UTF-8') print(data) testArgument('https://117.135.250.134:80')
相关文章推荐
- 一个使用了 osip 和 eXosip 库的 UAC 代理客户端的演示程序
- 【转】如何利用C#编写网页投票器程序|如何使用代理来投票|代理IP来投票
- 配置.NET程序使用代理进行HTTP请求
- python爬虫之Scrapy 使用代理配置
- 使用代理(WebProxy)爬虫
- 使用beautifulsoup写的第一个小爬虫程序
- 如何利用C#编写网页投票器程序 如何使用代理来投票 代理IP来投票
- 数据抓取之反爬虫规则:使用代理和http头信息
- 讲解Python的Scrapy爬虫框架使用代理进行采集的方法
- Python 爬虫入门(二)—— IP代理使用 - 不剃头的一休哥 - 博客园
- python爬虫之Scrapy 使用代理配置
- [python]新手写爬虫v2.5(使用代理的异步爬虫)
- Python 爬虫入门(二)—— IP代理使用
- 第八篇 SQL Server代理使用外部程序
- python使用代理爬虫例子
- SQL Server代理(8/12):使用SQL Server代理外部程序
- nginx 反向代理之使用特定目录和处理程序
- Python 爬虫入门(二)—— IP代理使用
- VMWare安装Ubuntu10.10 server版的NAT方式上网方法,以及apt-get使用公司代理安装程序的方法
- 使用PHP创建基本的爬虫程序【转】