下記の質問者さんと同じ内容のプログラムを作成しようとしています。
https://teratail.com/questions/152091
しかし、AttributeError: 'NoneType' object has no attribute 'decode'が
表示されてしまい進みません。アドバイスお願いします。
python
1import os 2import sys 3import traceback 4from mimetypes import guess_extension 5from time import time, sleep 6from urllib.request import urlopen, Request 7from urllib.parse import quote 8from bs4 import BeautifulSoup 9 10#任意のメールアドレス 11#ツールを使ってダウンロードするときのマナーみたい 12MY_EMAIL_ADDR = '@.com' 13 14class Fetcher: 15 def __init__(self, ua=''): 16 self.ua = ua 17 18 def fetch(self, url): 19 req = Request(url, headers={'User-Agent': self.ua}) 20 try: 21 with urlopen(req, timeout=3) as p: 22 b_content = p.read() 23 mime = p.getheader('Content-Type') 24 except: 25 sys.stderr.write('Error in fetching {}\n'.format(url)) 26 sys.stderr.write(traceback.format_exc()) 27 return None, None 28 return b_content, mime 29 30fetcher = Fetcher(MY_EMAIL_ADDR) 31 32def fetch_and_save_img(num): 33 num_self = num 34 #dataというディレクトリが作られる 35 data_dir = './image/enjyeruroad/' 36 if not os.path.exists(data_dir): 37 os.makedirs(data_dir) 38 39 img_urls = img_url_list(num_self)[0] 40 num_self2 = img_url_list(num_self)[1] 41 s = num_self2 - 21 42 43 for i, img_url in enumerate(img_urls): 44 sleep(0.1) 45 img, mime = fetcher.fetch(img_url) 46 if not mime or not img: 47 continue 48 ext = guess_extension(mime.split(';')[0]) 49 if ext in ('.jpe', '.jpeg'): 50 ext = '.jpg' 51 if not ext: 52 continue 53 result_file = os.path.join(data_dir, str(s) + ext) 54 with open(result_file, mode='wb') as f: 55 f.write(img) 56 s += 1 57 print('fetched', img_url) 58 59 if len(img_url) != 0: 60 fetch_and_save_img(num_self2) 61 62def img_url_list(num): 63 url = 'https://search.yahoo.co.jp/image/search?p=エンジェルロード&oq=&ei=UTF-8&xargs=2&b={}'.format(num) 64 byte_content, _ = fetcher.fetch(url) 65 soup = BeautifulSoup(byte_content.decode('UTF-8'), 'html.parser') 66 67 img_elems = soup.select('a[target=imagewin] > img') 68 print(img_elems) 69 70 img_urls = [e['src'] for e in img_elems] 71 print(img_urls) 72 73 return img_urls, num + 20 74 75if __name__ == '__main__': 76 # word = sys.argv[1] 77 # fetch_and_save_img(word) 78 num = 1 79 fetch_and_save_img(num)
下記がエラーコードです。
python
1エラーコード 2runfile('/Users/nakayakenta/get_images_yahoo.py') 3Error in fetching https://search.yahoo.co.jp/image/search?p=エンジェルロード&oq=&ei=UTF-8&xargs=2&b=1 4Traceback (most recent call last): 5 File "/Users/nakayakenta/get_images_yahoo.py", line 21, in fetch 6 with urlopen(req, timeout=3) as p: 7 File "/Users/nakayakenta/anaconda3/lib/python3.7/urllib/request.py", line 222, in urlopen 8 return opener.open(url, data, timeout) 9 File "/Users/nakayakenta/anaconda3/lib/python3.7/urllib/request.py", line 525, in open 10 response = self._open(req, data) 11 File "/Users/nakayakenta/anaconda3/lib/python3.7/urllib/request.py", line 543, in _open 12 '_open', req) 13 File "/Users/nakayakenta/anaconda3/lib/python3.7/urllib/request.py", line 503, in _call_chain 14 result = func(*args) 15 File "/Users/nakayakenta/anaconda3/lib/python3.7/urllib/request.py", line 1360, in https_open 16 context=self._context, check_hostname=self._check_hostname) 17 File "/Users/nakayakenta/anaconda3/lib/python3.7/urllib/request.py", line 1317, in do_open 18 encode_chunked=req.has_header('Transfer-encoding')) 19 File "/Users/nakayakenta/anaconda3/lib/python3.7/http/client.py", line 1229, in request 20 self._send_request(method, url, body, headers, encode_chunked) 21 File "/Users/nakayakenta/anaconda3/lib/python3.7/http/client.py", line 1240, in _send_request 22 self.putrequest(method, url, **skips) 23 File "/Users/nakayakenta/anaconda3/lib/python3.7/http/client.py", line 1107, in putrequest 24 self._output(request.encode('ascii')) 25UnicodeEncodeError: 'ascii' codec can't encode characters in position 20-27: ordinal not in range(128) 26Traceback (most recent call last): 27 28 File "<ipython-input-12-d5a5d83e6f61>", line 1, in <module> 29 runfile('/Users/nakayakenta/get_images_yahoo.py') 30 31 File "/Users/nakayakenta/anaconda3/lib/python3.7/site-packages/spyder_kernels/customize/spydercustomize.py", line 668, in runfile 32 execfile(filename, namespace) 33 34 File "/Users/nakayakenta/anaconda3/lib/python3.7/site-packages/spyder_kernels/customize/spydercustomize.py", line 108, in execfile 35 exec(compile(f.read(), filename, 'exec'), namespace) 36 37 File "/Users/nakayakenta/get_images_yahoo.py", line 79, in <module> 38 fetch_and_save_img(num) 39 40 File "/Users/nakayakenta/get_images_yahoo.py", line 39, in fetch_and_save_img 41 img_urls = img_url_list(num_self)[0] 42 43 File "/Users/nakayakenta/get_images_yahoo.py", line 65, in img_url_list 44 soup = BeautifulSoup(byte_content.decode('UTF-8'), 'html.parser') 45 46AttributeError: 'NoneType' object has no attribute 'decode'
補足情報(FW/ツールのバージョンなど)
python3.6.6
mac OS Mojava

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/01/07 04:36
2019/01/07 04:39