【发布时间】:2016-10-06 19:07:06
【问题描述】:
我尝试使用BeautifulSoup 4,安装成功后总是报错,无法修复“soup=BeautifulSoup(html)”
当我使用以下代码时:
from bs4 import BeautifulSoup
soup = BeautifulSoup(html)
它显示错误:
//anaconda/lib/python3.5/site-packages/bs4/__init__.py:166: UserWarning: No parser was explicitly specified, so I'm using the best available HTML parser for this system ("lxml"). This usually isn't a problem, but if you run this code on another system, or in a different virtual environment, it may use a different parser and behave differently.
To get rid of this warning, change this:
BeautifulSoup([your markup])
to this:
BeautifulSoup([your markup], "lxml")
markup_type=markup_type))
Traceback (most recent call last):
File "<ipython-input-13-d4b16f497b1d>", line 1, in <module>
runfile('/Users/beckswu/Desktop/coursera/using python access web data/class 2.py', wdir='/Users/beckswu/Desktop/coursera/using python access web data')
File "//anaconda/lib/python3.5/site-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 699, in runfile
execfile(filename, namespace)
File "//anaconda/lib/python3.5/site-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 88, in execfile
exec(compile(open(filename, 'rb').read(), filename, 'exec'), namespace)
File "/Users/beckswu/Desktop/coursera/using python access web data/class 2.py", line 37, in <module>
soup = BeautifulSoup(html)
File "//anaconda/lib/python3.5/site-packages/bs4/__init__.py", line 212, in __init__
markup, from_encoding, exclude_encodings=exclude_encodings)):
File "//anaconda/lib/python3.5/site-packages/bs4/builder/_lxml.py", line 108, in prepare_markup
markup, try_encodings, is_html, exclude_encodings)
TypeError: __init__() takes from 2 to 4 positional arguments but 5 were given
然后我将代码更改为
from bs4 import BeautifulSoup
soup = BeautifulSoup(html,"lxml")
markup_type=markup_type))
它也显示错误
markup_type=markup_type))
^
SyntaxError: invalid syntax
我该如何解决这个问题?我感谢任何人的帮助。
【问题讨论】:
-
你认为
markup_type=markup_type在做什么?
标签: web-scraping beautifulsoup python-3.5