- http要求を送信してWebページで開こうとしているローカルハードドライブにhtmlファイルがあります。
- httpリクエストが作成されると、私はurlを渡して保存されたhtmlファイルを解析しようとしています。(一度に1つのファイルを渡すと解析が成功しますが、ディレクトリ内のすべてのファイルに対して動的に行います。ループのために使用されるこのワークアウトはありません)
解析が行われた後、私は、JSONファイルにデータを保存しています(正常に動作します) 私はここのコードを貼り付けています。ディレクトリ内の複数のファイルを解析できません
import json import os from newspaper import Article import newspaper # initiating the server server_start = os.system('start "HTTP Server on port 8000" cmd.exe /c {python -m http.server}') http_server = 'http://localhost:8000/' links = '' path = "<path>" for f in os.listdir(path): if f.endswith('.html'): links = http_server + path + f blog_post = newspaper.build(links) for article in blog_post.articles: print(article.url) article = Article(links) article.download('') article.parse() data = {"HTML": article.html, "author": article.authors, "title": article.title, "text": article.text, "date": str(article.publish_date)} json_data = json.dumps(data) with open('data.json', 'w') as outfile: json.dump(data, outfile)
エラーメッセージ:
...\newspaper\Scripts\python.exe ".../parsing_newspaper/test1.py" [Source parse ERR] http://localhost:8000/.../cnnpolitics-russian.html Traceback (most recent call last):
File"...\newspaper\lib\site-packages\newspaper\parsers.py", line 68, in fromstring cls.doc = lxml.html.fromstring(html)
File "...\newspaper\lib\site-packages\lxml\html__init__.py", line 876, in fromstring doc = document_fromstring(html, parser=parser, base_url=base_url, **kw)
File "...\newspaper\lib\site-packages\lxml\html__init__.py", line 762, in document_fromstring value = etree.fromstring(html, parser, **kw)
File "src\lxml\lxml.etree.pyx", line 3213, in lxml.etree.fromstring (src\lxml\lxml.etree.c:78994)
File "src\lxml\parser.pxi", line 1848, in lxml.etree._parseMemoryDocument (src\lxml\lxml.etree.c:118325)
File "src\lxml\parser.pxi", line 1729, in lxml.etree._parseDoc (src\lxml\lxml.etree.c:116883)
File "src\lxml\parser.pxi", line 1063, in lxml.etree._BaseParser._parseUnicodeDoc (src\lxml\lxml.etree.c:110870)
File "src\lxml\parser.pxi", line 595, in lxml.etree._ParserContext._handleParseResultDoc (src\lxml\lxml.etree.c:105093)
File "src\lxml\parser.pxi", line 706, in lxml.etree._handleParseResult (src\lxml\lxml.etree.c:106801)
File "src\lxml\parser.pxi", line 646, in lxml.etree._raiseParseError (src\lxml\lxml.etree.c:105947)
File "", line 0 lxml.etree.XMLSyntaxError:
You must
download()
an article before callingparse()
on it!Traceback (most recent call last): File ".../test1.py", line 26, in article.parse()
File "...\newspaper\lib\site-packages\newspaper\article.py", line 168, in parse raise ArticleException() newspaper.article.ArticleException
1. HTMLは確かに空ではありません。 –
ご回答ありがとうございます@sdikby 1. htmlは空ではありません。 2.ウェブサイトからウェブページ(html)を直接保存しました。 3.さらにタスクでは、htmlファイルがscrapyを使用してWebからスクラップされ、Webページがlocaldiskに保存されます。記事がダウンロードされるかどうかわからないし、 "問題のある"記事の意味を理解できていない。 –
申し訳ありません私は何を意味するかを記述するために、より良い用語を見つけられませんでした。 「問題がある」とは、「新聞」が定義する何らかの理由(例外)でダウンロードできなかった記事を意味していました。あなたがテストできるクラスパラメータself._downloaded(私は思う)があります。 もしかしたら何かが足りなくて、私が提案したことはまったく当てはまりません。 – sdikby