
『python
クローリング&スクレイピング データ収集・解析のための実践開発のためのガイド』
p49で与えられたサンプルコードで正規表現によるスクレイピングをしようとしたところエラーが起きました。
コード import re from html import unescape with open ('dp.html') as f: html = f.read for partial_html in re.findall(r'<a itemprop="url".*?</ul>\s*</a></li>', html, re.DOTALL): url = re.search(r'<a itemprop="url" href="(.*?)">', partial_html).group(1) url = 'http://sample.scraping-book.com/dp' + url title = re.search(r'<p itemprop="name".*?</p>', partial_html).group(0) title = title.replace('<br/>', ' ') title = re.sub(r'<.*?>', '', title) title = unescape(title) print(url, title) `` 上記コードをscrape_re_py として保存してターミナルで実行したところ以下のようなエラーが出ました。 $ python Documents/scrape_re_py Traceback (most recent call last): File "Documents/scrape_re_py", line 9, in <module> for partial_html in re.findall(r'<a itemprop="url".*?</ul>\s*</a></li>', html, re.DOTALL): File "/Users/kobayashitakashi/anaconda3/lib/python3.7/re.py", line 223, in findall return _compile(pattern, flags).findall(string) TypeError: expected string or bytes-like object
どのように対処したらよいか教えてください。

回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2019/02/24 14:27
2019/02/24 14:33
2019/02/24 14:39
退会済みユーザー
2019/02/24 15:08