今日目标:使用自己的电脑进行一次爬虫
使用工具:
PyCharm
问题及解决方案:
小目录:
一、BS4库缺失
二、‘urllib’ has no attribute 'request’
三、bs4.FeatureNotFound: Couldn’t find a tree builder with the features you requested: lxml. Do you need to install a parser library?
四、can’t open file ‘kkk.py’: [Errno 2] No such file or directory
一、BS4库缺失
代码:from bs4 import BeautifulSoup
bs4库被下划线提示,分析为bs4库没有正常导入,致使BeautifulSoup工具无法正常使用。
通过查阅网络资料,得知是电脑上缺失bs4库,需要下载安装,解决方法如下:
以管理员身份运行 cmd ,输入 命令
python -m pip install bs4
运行结果如下图,下载并安装bs4成功。
重启PyCharm,红色下划线消失。【1】
二、‘urllib’ has no attribute 'request’
运行昨天在虚拟机上使用的代码,提示urllib没有request,
这是因为import urllib导入包时,其他什么都做不了。因此导入的时候:
import urllib.request
然后该问题消失。【2】
三、bs4.FeatureNotFound: Couldn’t find a tree builder with the features you requested: lxml. Do you need to install a parser library?
原代码为:
soup=BeautifulSoup(html,‘lxml’)
改为:
soup=BeautifulSoup(html,‘html.parser’)
问题解决。【3】
四、can’t open file ‘kkk2.py’: [Errno 2] No such file or directory
自己遇到该问题的原因:
Python的环境变量出错。
网络上其他人遇到该问题的其他原因:
文件、文件夹命名不可以使用空格,例如"Python Files"中的空格就是错误的。
Python环境配置:
操作见【4】。但是idle可能由于使用微软商店安装,不论是开始栏搜索还是任务管理器右键进程,都不能打开文件所在位置。于是我通过在c盘搜索:idle,最后找到。
成果展示:
爬取成功。
参考来自:
【1】、https://blog.csdn.net/zhaomengszu/article/details/79404644
【2】、https://blog.csdn.net/tyro_blog/article/details/103146992
【3】、https://blog.csdn.net/weixin_44024857/article/details/87904685?utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-1.control&dist_request_id=1619700915752_59671&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-1.control
【4】、https://jingyan.baidu.com/article/f25ef25481095d482c1b8204.html
侵权删。