使用代理的爬虫小程序

使用使用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')


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值