JSONでWEBスクレイピングをしようとしているのですが、やり方がわかりません。
python3
1import json 2import urllib 3 4url = 'http://xxx.com' 5headers = { 6 "User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:47.0) Gecko/20100101 Firefox/47.0", 7 } 8request = urllib.request.Request(url=url, headers=headers) 9response = urllib.request.urlopen(request) 10root = json.loads(response.read()) 11print('caution=' + root['copyright'])
というコードを書いたのですが、
request = urllib.request.Request(url=url, headers=headers)
AttributeError: 'module' object has no attribute 'request'
と出てしまいます。requestパッケージも入れましたが反応しません。どのように解決できますでしょうか?
追記
また、このようなコードでも実行致しました。
python3
1import json 2import urllib.request 3 4url = 'http://xxx.com' 5headers = { 6 "User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:47.0) Gecko/20100101 Firefox/47.0", 7 } 8request = urllib.request.Request(url=url, headers=headers) 9response = urllib.request.urlopen(request) 10root = json.loads(response.read()) 11print('caution=' + root['copyright'])
そうしますと、以下のような結果になりました。
import urllib.request
ImportError: No module named request
回答4件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/05/07 04:26
2019/05/07 04:38
2019/05/07 06:16
2019/05/07 07:13
2019/05/07 07:32
2019/05/07 08:15