一、安装库

需要安装有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

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


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

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

链接:https://blog.csdn.net/weixin_43848422/article/details/109246324

三、利用bs4库解析数据

链接:https://blog.csdn.net/weixin_43848422/article/details/109246523

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

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

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


出现报错: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

 

相关文章:

  • 2021-04-06
  • 2022-12-23
  • 2021-12-03
  • 2021-04-10
  • 2021-08-01
  • 2021-06-19
  • 2022-12-23
  • 2022-01-08
猜你喜欢
  • 2021-05-06
  • 2022-12-23
  • 2021-08-22
  • 2022-01-03
  • 2021-12-25
  • 2021-08-27
相关资源
相似解决方案