**
标题: 解决BeautifulSoup库运行时报错问题
**
正确代码:
from urllib.request import urlopen
from bs4 import BeautifulSoup
html = urlopen("http://www.pythonscraping.com/pages/page1.html")
bsObi = BeautifulSoup(html.read(), 'html.parser')
print(bsObi.h1)
正确结果:
C:\Users\user\AppData\Local\Programs\Python\Python37\python.exe C:/Users/user/PycharmProjects/pythonProject/11.py
<h1>An Interesting Title</h1>
Process finished with exit code 0
问题1:SyntaxError: invalid character in identifier
from urllib.request import urlopen
from bs4 import BeautifulSoup
html = urlopen("http://www.pythonscraping.com/pages/page1.html")
bsObi = BeautifulSoup(html.read(), ‘html.parser’)
print(bsObi.h1)
C:\Users\user\AppData\Local\Programs\Python\Python37\python.exe C:/Users/user/PycharmProjects/pythonProject/11.py
File "C:/Users/user/PycharmProjects/pythonProject/11.py", line 4
bsObi = BeautifulSoup(html.read(), ‘html.parser’)
^
SyntaxError: invalid character in identifier
Process finished with exit code 1
原因分析:
代码第4行的bsObi = BeautifulSoup(html.read(), ‘html.parser’)
中的引号是中文输入法输入
解决方案:
将代码第4行的bsObi = BeautifulSoup(html.read(), ‘html.parser’)
中的引号改成英文输入下的引号
问题2:出现None
,无法找到h1标签的源代码
from urllib.request import urlopen
from bs4 import BeautifulSoup
html = urlopen("http://www.pythonscraping.com/pages/page1.html")
bsObi = BeautifulSoup(html.read(), 'html.parser')
print(bsObi.hl)
C:\Users\user\AppData\Local\Programs\Python\Python37\python.exe C:/Users/user/PycharmProjects/pythonProject/11.py
None
Process finished with exit code 0
原因分析:
标签名称出错,将h1标签名称打错为hl
解决方案:
将hl修改为h1
问题3:弹出界面出现Error: Please select a valid Python interpreter
原因分析:
在pycharm导入源项目的时候没有选择运行Python的程序
解决方案:
打开settings(CTRL + ALT + S)或者file>settings,打开配置框,如下图:
选择 Project pythonProject 中的 Python Interpreter
进入 Python Interpreter 界面后,点击下方红框处的右侧机械按钮
然后点击 Add… 选项
进入Add Python Interpreter 界面选择 System Interpreter 点击下方红框处
选择你的安装地址选择 python.exe添加到 Interpreter 中
之后就选择确定【OK】就好了