Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
558 views
in Technique[技术] by (71.8m points)

请教各位,python编写爬虫,返回http error 521怎么解决

import urllib.request
import urllib.parse
import urllib.error
import re
import random
def open_url(url):

    req_header = {'Connection': 'Keep-Alive',
            'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:52.0) Gecko/20100101 Firefox/52.0',
            'Accept':'*/*',
            'Accept-Encoding': 'gzip, deflate',
            'Accept-Language': 'zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3'}
    req = urllib.request.Request(url, None, req_header)
    with urllib.request.urlopen(req, None, timeout=5) as respond:
        html = respond.read()
    return html


def get_proxy_urls():
    proxy_list = []
    for i in range(1,11):
        str1 = r'http://www.kuaidaili.com/proxylist/%d/' % i
        proxy_list.append(str1)
    return proxy_list


def get_ip(url):
    comp = '''<td data-title="IP">(.+?)</td>
            <td data-title="PORT">(.+?)</td>
            <td data-title="匿名度">(.+?)</td>
            <td data-title="类型">(.+?)</td>
            <td data-title="get/post支持">(.+?)</td>
            <td data-title="位置">(.+?)</td>
            <td data-title="响应速度">(.+?)</td>
            <td data-title="最后验证时间">(.+?)</td>'''
    text_html = open_url(url).decond('utf_8')
    ip_list = re.findall(comp, text_html)
    return ip_list


def test():
    url = random.choice(get_proxy_urls())
    print(url)
    print(open_url(url))
    print(get_ip(url))
if __name__ == '__main__':
    test()

在urllib.request.urlopen()处出现异常
urllib.error.HTTPError: HTTP Error 521:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

Please log in or register to answer this question.

Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...