###前提・実現したいこと
「PythonによるWebスクレイピング(オライリー・ジャパン)」を使っていて、本書通りのコードを打ったところ、以下のエラーが発生しました。
###発生している問題・エラーメッセージ
Traceback (most recent call last): File "webscraping.py", line 4, in <module> html = urlopen("http://www.pythonscraping.com/pages/warandpeace.html", "html.parser") File "/Users/nakasemidori/.pyenv/versions/3.5.0/lib/python3.5/urllib/request.py", line 162, in urlopen return opener.open(url, data, timeout) File "/Users/nakasemidori/.pyenv/versions/3.5.0/lib/python3.5/urllib/request.py", line 463, in open req = meth(req) File "/Users/nakasemidori/.pyenv/versions/3.5.0/lib/python3.5/urllib/request.py", line 1170, in do_request_ raise TypeError(msg) TypeError: POST data should be bytes or an iterable of bytes. It cannot be of type str.
###該当のソースコード
python
1from urllib.request import urlopen 2from bs4 import BeautifulSoup 3 4html = urlopen("http://www.pythonscraping.com/pages/warandpeace.html", "html.parser") 5bs0bj = BeautifulSoup(html) 6 7nameList = bs0bj.findAll("span", {"class": "green"}) 8for name in nameList: 9 print(name.get_text())
###試したこと
エラー文は、POST dataはstr型ではなくbytes型ではないといけないというエラーだと思い、
nameList = bs0bj.findAll("span", {"class": "green"})
の部分を
nameList = bs0bj.findAll("span", {"class": "green"}.encode("utf-8"))
に書き換えても同じエラーがでてしまいます。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/05/29 06:55