一、安装库
需要安装有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
这篇博客记录了Python爬虫过程中遇到的问题,包括安装库时的错误(需求库名应为requests而非request)、使用requests库爬取网页时的错误(header拼写错误)以及如何利用bs4库解析数据。博主通过实例演示了爬取豆瓣电影Top250数据的过程。
2096

被折叠的 条评论
为什么被折叠?



