账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    python3 爬取网页失败
    • 2020-01-01 00:00
    • 10
    38
    0
    import re
    import urllib.request
    from bs4 import BeautifulSoup
    import ssl
    
    ssl._create_default_https_context = ssl._create_stdlib_context
    
    def getcontent(url,page):
    
        headers = {'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36'}
    
        req = urllib.request.Request(url=url,headers=headers)
    
        res = urllib.request.urlopen(req).read().decode('utf-8')
    
        print(res)
    
    
    
    for i in range(1):
    
        url = 'http://www.qiushibaike.com/8hr/page/'+str(i) +'/'
        getcontent(url,i)
    

    请问一下大佬 我这是哪里错误了吗? 爬取的网页一直糗百的错误页面

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>糗百君的飞船出了一点小毛病</title>
    <style>
    body {
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    background-color: #2e344a;
    color: #fff;
    font-size: 14px;
    text-align: center;
    font-family: arial, sans-serif;
    }
    .dialog {
    position: absolute;
    left: 50%;
    bottom: 100px;
    margin-left: -120px;
    text-align: center;
    z-index: 100;
    }
    h1 {
    font-size: 16px;
    color: #fff;
    line-height: 1.5em;
    /* padding-top: 360px; */
    }
    a:link { text-decoration: none; color: #ff9900 }
    a:active { text-decoration:blink }
    a:hover { text-decoration: none; color: #ff9900 }
    a:visited { text-decoration: none; color: #ff9900 }
    </style>
    </head>
    <body>
    <iframe width="100%" height="100%" allowtransparency="true" style="background-color:transparent" frameborder="0" src="https://editor.3dpunk.com/editor3?oid=yE51u114009rKS7F&mode=1&transparent=1&startMovie=0&zoom=0&showLoading=0&toolBar=0"></iframe>
    <div class="dialog">
    <h1>糗百君的飞船出了一点小毛病……</h1>
    <p>莫慌, 点击<a href="https://www.qiushibaike.com"> 这里</a> 可以找到出路</p>
    </div>
    <script>
    var _hmt = _hmt || [];
    (function () {
    var hm = document.createElement("script");
    hm.src = "//hm.baidu.com/hm.js?18a964a3eb14176db6e70f1dd0a3e557";
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(hm, s);
    })();
    </script>
    </body>
    </html>
    0
    打赏
    收藏
    点击回答
    您的回答被采纳后将获得:提问者悬赏的 10 元积分
        全部回答
    • 0
    • 暗云飞飞 普通会员 1楼

      如果你在Python 3中尝试爬取网页失败,你可以尝试以下几个步骤来排查问题:

      1. 检查网络连接:确保你的网络连接是正常的,没有断开或延迟。

      2. 检查网络请求:确保你的网络请求是正常的,没有错误的URL。你也可以尝试使用不同的URL来检查是否正确。

      3. 检查代码:检查你的代码是否有错误,例如语法错误、拼写错误或逻辑错误。你可以使用Python的IDE(如PyCharm、VS Code等)来运行你的代码并查看错误信息。

      4. 检查缓存:检查你的浏览器是否缓存了错误的网页。你可以在浏览器的设置中找到清除缓存的方法。

      5. 使用代理:如果你的网站有反爬虫机制,你可能需要使用代理来提高爬取的效率。

      6. 查看日志:如果你的代码在某些地方打印了大量的错误信息,那么这可能是问题所在。你可以在Python的的日志模块中查看这些日志。

      7. 查看Python版本:确保你的Python版本与目标网站的版本兼容。

      如果以上步骤都不能解决问题,你可能需要寻找更专业的帮助。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部