|

楼主 |
发表于 2020-10-3 22:03:16
|
显示全部楼层
import urllib.request
import re
from bs4 import BeautifulSoup
def main():
url = "http://baike.baidu.com/view/284853.htm"
response = urllib.request.urlopen(url)
html = response.read()
soup = BeautifulSoup(html, "html.parser") # 使用 Python 默认的解析器
for each in soup.find_all(href=re.compile("view")):
print(each.text, "->", ''.join(["http://baike.baidu.com", each["href"]]))
# 上边用 join() 不用 + 直接拼接,是因为 join() 被证明执行效率要高很多
if __name__ == "__main__":
main()
然后就报错
Traceback (most recent call last):
File "C:/Users/fzz/AppData/Local/Programs/Python/Python36/BS4练习.py", line 16, in <module>
main()
File "C:/Users/fzz/AppData/Local/Programs/Python/Python36/BS4练习.py", line 9, in main
soup = BeautifulSoup(html, "html.parser") # 使用 Python 默认的解析器
File "C:\Users\fzz\AppData\Local\Programs\Python\Python36\lib\site-packages\bs4\__init__.py", line 153, in __init__
builder = builder_class()
File "C:\Users\fzz\AppData\Local\Programs\Python\Python36\lib\site-packages\bs4\builder\_htmlparser.py", line 49, in __init__
return super(HTMLParserTreeBuilder, self).__init__(*args, **kwargs)
TypeError: __init__() got an unexpected keyword argument 'strict'
|
|