2016-02-12 61 views
0

我剛剛買了一本書,告訴我如何刮網站,但第一個例子了蝙蝠的權利是不是爲我工作 - 所以現在我有點不高興,我首先買了這本書,但我想嘗試去實現它。Python的BeautifulSoup錯誤BsObj

在Python 3.5我的代碼:

from urllib.request import urlopen 
from bs4 import BeautifulSoup 
html = urlopen("http://www.pythonscraping.com/pages/page1.html") 
BsObj = BeautifulSoup(html.read()) 
print(bsObj.h1)* 

以下是錯誤我得到

Traceback (most recent call last):

File "C:/Users/MyName/AppData/Local/Programs/Python/Python35-32/Lib/site-packages/bs4/test.py", line 5, in BsObj = BeautifulSoup(html.read())

File "C:\Users\MyName\AppData\Local\Programs\Python\Python35-32\lib\site-packages\bs4__init__.py", line 153, in init builder = builder_class()

File "C:\Users\MyName\AppData\Local\Programs\Python\Python35-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'

任何想法將是超級有用嗎? 在此先感謝

回答

0

我猜你從書上轉錄的代碼。 bsObj沒有被一致地命名,並且在print()之後有不必要的*。它會在你改變這兩件事情之後起作用。

還要注意,不需要read(),它是更好地定義解析器,否則你會得到一個警告。

from urllib.request import urlopen 
from bs4 import BeautifulSoup 
html = urlopen('http://www.pythonscraping.com/pages/page1.html') 
bsObj = BeautifulSoup(html, 'html.parser') 
print(bsObj.h1) 
+0

謝謝 - 關於錯字,我複製了我的最新文件,我改變了拼寫 - 我做了兩個建議,但仍然出現錯誤。 – PatrickP76

+0

同樣的錯誤? –

+0

是我最初發布的同一個錯誤。我不得不爲這本書下載Python 35,並且之前使用了27版 - 這導致了我無法修復或找到修復程序的問題,因此我卸載了這兩個版本並重新加載了35 - 我的直覺告訴我在安裝35或BS時做了一些錯誤。感謝您的幫助 – PatrickP76

0

嘿,你只是有一些拼寫錯誤BsObj不bsObj在打印行。

from urllib.request import urlopen 
from bs4 import BeautifulSoup 

html = urlopen("http://www.pythonscraping.com/pages/page1.html") 
BsObj = BeautifulSoup(html.read()) 
print(BsObj.h1) 
+0

bsObj = BeautifulSoup(HTML,「html.parser」) 打印(bsObj.h1) – PatrickP76

+0

這是我改變了: – PatrickP76

+0

高興有幫助;) – Seekheart