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)
结果是错误的
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
发布于 2017-06-15 05:35:33
一个简短的修正更改打印(Soup.title)到打印(soup.title)汤是小写。尝试从urllib切换到请求。希望这有帮助,如果没有,请告诉我们。
编辑1:添加结果的图像
发布于 2017-06-15 05:04:50
您必须更改该字符串:
soup = BeautifulSoup(thepage.read(), "html.parser")
发布于 2017-06-15 05:49:32
我拿到输出了,
我发现的区别是,打印(soup.title)#在这里使用大写汤
如果没有解决,重新安装您的美丽汤到适当的版本。您需要版本4.4+,因为您使用python 3.6+。
import urllib.request
是不需要的,因为您已经import urllib
了
您使用的是python3,所以专门为您的python3安装bs4,
pip3 install beautifulsoup4
如果您没有让pip3
使用apt-get install python3-pip
安装它
https://stackoverflow.com/questions/44558887
复制相似问题