Python爬虫類入門で出会った穴

3458 ワード

1.環境
-Python mac osプリインストールpython
$ python -V  
Python 2.7.10
$ where python
/usr/bin/python
$ ls /System/Library/Frameworks/Python.framework/Versions
2.3     2.5     2.6     2.7     Current
$ ls /Library/Frameworks/Python.framework/Versions (       )

-IDE Pycharm-補助取付pip
sudo easy_install pip

-Pythonライブラリ
sudo pip install requests (    requests 2.13.0) 
sudo pip install BeautifulSoup ( BeautifulSoup 3.2.1)
sudo pip install lxml ( lxml 3.7.3)

 
2.質問
-質問1
コード:soup=BeautifulSoup(html,'lxml')エラー:Traceback(most recent call last):File"/Users/cuizhenyu/Documents/Codes/Python/DownloadMeitu/LibBeautifulSoupTest.py",line 15,In soup=BeautifulSoup(html)#soup=BeautifulSoup(html,'lxml')TypeError:'module'object is not callable解決:from BeautifulSoup import BeautifulSoup
-質問2
コード:soup=BeautifulSoup(html,'lxml')エラー:Traceback(most recentcall last):File"/Users/cuizhenyu/Documents/Codes/Python/DownloadMeitu/LibBeautifulSoupTest.py",line 15,in soup=BeautifulSoup(html,'lxml')#soup=BeautifulSoup(html,'lxml')エラーFile"/Library/Python/2.7.7/site-packages/BeautifulSoup.py",line 1522,in_init__ BeautifulStoneSoup.__init__(self, *args, **kwargs) File "/Library/Python/2.7/site-packages/BeautifulSoup.py", line 1147, in __init__ self._feed(isHTML=isHTML) File "/Library/Python/2.7/site-packages/BeautifulSoup.py", line 1189, in _feed SGMLParser.feed(self, markup) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/sgmllib.py", line 104, in feed self.goahead(0) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/sgmllib.py", line 138, in goahead k = self.parse_starttag(i) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/sgmllib.py", line 296, in parse_starttag self.finish_starttag(tag, attrs) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/sgmllib.py", line 338, in finish_starttag self.unknown_starttag(tag, attrs) File "/Library/Python/2.7/site-packages/BeautifulSoup.py", line 1338, in unknown_starttag self.endData()File"/Library/Python/2.7/site-packages/BeautifulSoup.py",line 1251,in endData(not self.parseOnlyThese.text orAttributeError:'str'object has no attribute'text'解決:現在のBeautifulSoupはv 3版でlxmlなどはサポートされておらず、v 4版が必要です.
 
転載先:https://www.cnblogs.com/mulisheng/p/6665350.html