爬虫程序(一)---读取网页

读取网页时候,如果访问速度过快,会返回个timeout错误(10054),因此要在此做个try,并启用下一个代理。(代理可以百度,http 代理,要有端口号)。同时模拟浏览器,可以防止一些返回错误。

 

#读取网页函数
def FormatHTML( url ):
    flag = True
    count = 0
    sleep_download_time = 0
    time_out = 10
    fails = 0
    HTTP_num = 0
    HTTP_dl = ['211.142.236.132:80', '118.186.9.21:80', '118.186.9.22:80', '211.142.236.132:80']
    while True:
        if fails >= 3:
            return None
            break
        try:
            print u'========开启代理========='
            opener = urllib2.build_opener( urllib2.ProxyHandler( {'http':HTTP_dl[HTTP_num]} ), urllib2.HTTPHandler( debuglevel = 1 ) )
            urllib2.install_opener( opener )
            while flag:
                try:
                    print u'=========模拟浏览器========='
                    i_headers = {"User-Agent": "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1) Gecko/20090624 Firefox/3.5", "Referer": 'http://www.baidu.com'}
                    req = urllib2.Request( url, headers = i_headers )
                    time.sleep( sleep_download_time )
                    print u'==========读取网页==========='
                    f = urllib2.urlopen( req, timeout = time_out )
                    flag = False
                except urllib2.HTTPError, e:  
                    if e.code == 404:
                        print 'e.code:' + str( e.code ) 
                        count += 1
                        print 'count=' + str( count )
                        if count >= 4:
                            print 'count==' + str( count )
                            flag = False
                            return None 
                    else:
                        sleep_download_time = sleep_download_time + 2
                        time.sleep( sleep_download_time )
                        count += 1
                        print 'urllib2.HTTPError:' + str( e.code )
                        s = sys.exc_info()
                        print s
                        print "Error '%s' happened on line %d" % ( s[1], s[2].tb_lineno )
                        if count == 10:
                            flag = False
                            return None
                except :
                    print 2
                    sleep_download_time = sleep_download_time + 2
                    time.sleep( sleep_download_time )
                    count += 1
                    print url
                    print u"连接超时!"
                    s = sys.exc_info()
                    print "Error '%s' happened on line %d" % ( s[1], s[2].tb_lineno )
                    if count == 10:
                        flag = False
                        return None          
            reader = BeautifulSoup( f.read() ) 
            print u'==========读取完毕===========' 
            f.close()
            break
        except():
            HTTP_num += 1
            s = sys.exc_info()
            print "Error '%s' happened on line %d" % ( s[1], s[2].tb_lineno )
            fails += 1
            time_out += 5
    return reader


 




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值