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

这篇博客记录了Python爬虫过程中遇到的问题,包括安装库时的错误(需求库名应为requests而非request)、使用requests库爬取网页时的错误(header拼写错误)以及如何利用bs4库解析数据。博主通过实例演示了爬取豆瓣电影Top250数据的过程。
部署运行你感兴趣的模型镜像

一、安装库

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


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

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

链接:https://blog.youkuaiyun.com/weixin_43848422/article/details/109246324

三、利用bs4库解析数据

链接:https://blog.youkuaiyun.com/weixin_43848422/article/details/109246523

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

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


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

 

您可能感兴趣的与本文相关的镜像

Stable-Diffusion-3.5

Stable-Diffusion-3.5

图片生成
Stable-Diffusion

Stable Diffusion 3.5 (SD 3.5) 是由 Stability AI 推出的新一代文本到图像生成模型,相比 3.0 版本,它提升了图像质量、运行速度和硬件效率

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值