zoukankan      html  css  js  c++  java
  • 【Python】解决urllib返回http error 503问题

    想自动下载一些网页,写了个简单的爬虫,但是碰到了503问题。

    代码是下面这个样子:

        site = "http://XXXX"
        local_filename, headers = urllib.request.urlretrieve(site)
        html = open(local_filename, encoding='UTF-8')
    
        soup = bs4.BeautifulSoup(html)
        names = soup.find_all('a')

    过去都会像上面这样写,不过这次调用urlretrieve的时候返回了503,解决方法是用下面这样的代码,模拟浏览器的行为去访问。

        hdr = {
            'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36',
            'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
            'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
            'Accept-Encoding': 'gzip, deflate, br',
            'Accept-Language': 'zh-CN,zh;q=0.9',
            'Connection': 'keep-alive'}
    
        site = "https://XXXX"
    
        r = urllib.request.Request(site, headers=hdr)
        response = urllib.request.urlopen(r)
        page = response.read()
    
        buff = BytesIO(page)
        f = gzip.GzipFile(fileobj=buff)
        res = f.read().decode('utf-8')
    
        soup = bs4.BeautifulSoup(res, 'html.parser')
        names = soup.find_all('a')
  • 相关阅读:
    双机信任关系
    VCS双机原理
    VCS常用指令
    TCL数组
    TCL数据类型
    TCL列表
    TCL基本语法
    CentOS防火墙中端口的开启和关闭
    会话保持技术及原理技术
    ESN
  • 原文地址:https://www.cnblogs.com/tiandsp/p/13783038.html
Copyright © 2011-2022 走看看