2017-06-15 54 views
0
import urllib 
import urllib.request 
from bs4 import BeautifulSoup 

theurl = "https://twitter.com/official_YDP09" 
thepage = urllib.request.urlopen(theurl) 
soup = BeautifulSoup(thepage, "html.parser") 

print(Soup.title) 

,其结果是错误BeautifulSoup错误

C:\Users\Yohan\AppData\Local\Programs\Python\Python36-32\python.exe "C:/Users/Yohan/PycharmProjects/Project/aku pasti bisa.py" 
Traceback (most recent call last): 
    File "C:/Users/Yohan/PycharmProjects/Project/aku pasti bisa.py", line 7, in <module> 
    soup = BeautifulSoup(thepage, "html.parser") 
    File "C:\Users\Yohan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\bs4\__init__.py", line 153, in __init__ 
    builder = builder_class() 
    File "C:\Users\Yohan\AppData\Local\Programs\Python\Python36-32\lib\site-packages\bs4\builder\_htmlparser.py", line 39, in __init__ 
    return super(HTMLParserTreeBuilder, self).__init__(*args, **kwargs) 
TypeError: __init__() got an unexpected keyword argument 'strict' 

Process finished with exit code 1 

回答

0

短修复更换打印(Soup.title)打印(soup.title ) 汤是小写。 尝试从urllib切换到requests。希望这有助于,如果不是,请让我们知道。

编辑1:结果enter image description here

+0

我有尝试,其结果是: 回溯(最近最后一次通话): 文件“test.py”,3号线,在 从BS4进口BeautifulSoup ModuleNotFoundError:否模块命名为'bs4' –

+0

检查我的答案@YohanesDwiPratama,因为你使用python 3你需要再次安装bs4 –

0

你必须改变该字符串:

soup = BeautifulSoup(thepage.read(), "html.parser") 
+0

我有尝试,但仍然错误 –

0

我得到了输出的图像添加,

差我发现了, 打印(soup.title)#在这里如果你不使用资本汤

解决重新安装到适当的版本你的美丽。你需要4.4或更高版本,因为您在使用使用python 3.6+

import urllib.request 

是不需要的,因为你已经import urllib

您使用python3,所以安装BS4专门为您python3,

pip3 install beautifulsoup4 

如果您没有pip3请使用apt-get install python3-pip

Output

相关问题