Python爬虫关于过程中错误及问题的记录!

326 阅读1分钟

一、安装库

需要安装有bs4、re、xlwt、sqlite3和requests
问题一:pip install request提示报错
ERROR: Could not find a version that satisfies the requirement request (from versions: none)
ERROR: No matching distribution found for request

![](https://p6-tt-ipv6.byteimg.com/origin/pgc-image/f5a6fce142254b769ebe736ab236aa5b)

原因:需要安装的库是requests,不是request!!!

二、利用requests库爬取网页获取数据

链接:blog.csdn.net/weixin_4384…

三、利用bs4库解析数据

链接:blog.csdn.net/weixin_4384…

四、实例:爬虫豆瓣电影Top250数据

1.利用requests库得到指定一个URL的网页内容

![](https://p6-tt-ipv6.byteimg.com/origin/pgc-image/49706ca164074491bc29b3447d4b6018)

出现报错:request() got an unexpected keyword argument ‘header’
原因:关键字‘header’错误,应为**‘headers’**
修正后代码为:

# 得到指定一个URL的网页内容
def askURL(url):
    # 用户代理,发送请求的时候,让豆瓣服务端理解这个是一个浏览器而不是爬虫(本质上告诉浏览器,我们可以接收什么水平的文件内容)
    head={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36'}
    html=''
    try:
        responses=requests.get(url,headers=head)
        responses.encoding='utf-8'
        html=responses.text
        print responses.status_code
        print html
    except Exception as e:
        print e