python 小白,Java程序员闲来无事研究了下python爬虫,在执行如下代码:
import requests
from bs4 import BeautifulSoup
url = 'http://www.weather.com.cn/weather/101230101.shtml'
header = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) '
'AppleWebKit/537.36 (KHTML, like Gecko) '
'Chrome/69.0.3486.0 Safari/537.36'}
response = requests.get(url=url, headers=header)
response.encoding = 'utf-8'
print(response.text)
soup = BeautifulSoup(response.text, "lxml")
print(soup)
遇到了如下问题:
Traceback (most recent call last):
File "D:/PycharmProjects/WeatherCapture/weatherCN.py", line 13, in <module>
soup = BeautifulSoup(response.text, "lxml")
File "D:\PycharmProjects\WeatherCapture\venv\lib\site-packages\bs4\__init__.py", line 208, in __init__
% ",".join(features))
bs4.FeatureNotFound: Couldn't find a tree builder with the features you requested: lxml. Do you need to install a parser library?
在网上搜索到的方法是执行命令:
pip3 install lxml
执行没问题。
可是重新执行python依旧报错。
解决办法:
1.点击屏幕右下方的python,选择Interpreter Setting…
2.在弹出的窗口中点“+”
3.在点击“+”后弹出的新窗体输入框输入“lxml”,之后点击左下角的 Install Package提示
表示已经OK了。
4.关闭弹出窗体,重新执行python代码,已经不报错了。