Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 无法分析目录中的多个文件 我试图通过发送http请求在网页中打开本地硬盘上的html文件 创建http请求后,我将通过传递url来解析存储的html文件:(一次传递一个文件时解析成功,但我希望对循环中使用的目录中的所有文件进行动态解析。这不起作用)_Python_Lxml_Python Newspaper - Fatal编程技术网

Python 无法分析目录中的多个文件 我试图通过发送http请求在网页中打开本地硬盘上的html文件 创建http请求后,我将通过传递url来解析存储的html文件:(一次传递一个文件时解析成功,但我希望对循环中使用的目录中的所有文件进行动态解析。这不起作用)

Python 无法分析目录中的多个文件 我试图通过发送http请求在网页中打开本地硬盘上的html文件 创建http请求后,我将通过传递url来解析存储的html文件:(一次传递一个文件时解析成功,但我希望对循环中使用的目录中的所有文件进行动态解析。这不起作用),python,lxml,python-newspaper,Python,Lxml,Python Newspaper,解析完成后,我会将数据保存到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://loca

解析完成后,我会将数据保存到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)
导入json
导入操作系统
从报纸进口文章
进口报纸
#启动服务器
server_start=os.system('start“端口8000上的HTTP服务器”cmd.exe/c{python-m HTTP.server}')
http_服务器http://localhost:8000/'
链接=“”
path=“”
对于os.listdir(路径)中的f:
如果f.endswith('.html'):
links=http_服务器+path+f
blog\u post=paper.build(链接)
对于blog_post.articles中的文章:
打印(article.url)
文章=文章(链接)
文章下载(“”)
第条解析()
data={“HTML”:article.HTML,“author”:article.authors,“title”:article.title,“text”:article.text,“date”:str(article.publish_date)}
json_data=json.dumps(数据)
以open('data.json','w')作为输出文件:
json.dump(数据,输出文件)
错误消息:

…\paper\Scripts\python.exe“../parsing_paper/test1.py” [源解析错误] 回溯(最近一次呼叫最后一次):

文件“…\paper\lib\site packages\paper\parsers.py”,第68行, 在fromstring cls.doc=lxml.html.fromstring(html)中

文件“…\paper\lib\site packages\lxml\html\uuuuu init\uuuuuuu.py”,第行 876,在fromstring doc=document_fromstring(html,parser=parser, 基本url=基本url,**千瓦)

文件“…\paper\lib\site packages\lxml\html\uuuuu init\uuuuuuu.py”,第行 762,在文档中_fromstringvalue=etree.fromstring(html,解析器, **千瓦)

文件“src\lxml\lxml.etree.pyx”,第3213行,在lxml.etree.fromstring中 (src\lxml\lxml.etree.c:78994)

文件“src\lxml\parser.pxi”,第1848行,在 lxml.etree.\u parseMemoryDocument(src\lxml\lxml.etree.c:118325)

文件“src\lxml\parser.pxi”,第1729行,在lxml.etree.\u parseDoc中 (src\lxml\lxml.etree.c:116883)

文件“src\lxml\parser.pxi”,第1063行,在 lxml.etree.\u BaseParser.\u parseUnicodec (src\lxml\lxml.etree.c:110870)

文件“src\lxml\parser.pxi”,第595行,在 lxml.etree.\u ParserContext.\u handleParseResultDoc (src\lxml\lxml.etree.c:105093)

文件“src\lxml\parser.pxi”,第706行,在 lxml.etree.\u handleParseResult(src\lxml\lxml.etree.c:106801)

lxml.etree中的文件“src\lxml\parser.pxi”,第646行 (src\lxml\lxml.etree.c:105947)

文件“”,第0行lxml.etree.xmlsyntaxer错误:

在调用
parse()
之前,您必须
download()
一篇文章

回溯(最近一次调用last):文件“../test1.py”,第26行,在 第条解析()

文件“…\paper\lib\site packages\paper\article.py”,第168行, 在parse raise articleeexception()paper.article.articleeexception中


深入调试前要遵循的检查列表:

  • 检查html是否为空
  • 检查ahtml是否“格式良好”
  • 检查文章是否为空
  • 检查是否下载了文章(函数parse()的作用是这样的,但这有助于隔离“有问题的”文章)

  • 不知道这是否有帮助,但请尝试以下方法:

    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)
    
    导入json
    导入操作系统
    从报纸进口文章
    进口报纸
    #启动服务器
    server_start=os.system('start“端口8000上的HTTP服务器”cmd.exe/c{python-m HTTP.server}')
    http_服务器http://localhost:8000/'
    链接=“”
    path=“”
    对于os.listdir(路径)中的f:
    如果f.endswith('.html'):
    links=http_服务器+path+f
    blog\u post=paper.build(链接)
    对于blog_post.articles中的文章:
    打印(article.url)
    文章=文章(链接)
    文章下载(“”)
    第条解析()
    data={“HTML”:article.HTML,“author”:article.authors,“title”:article.title,“text”:article.text,“date”:str(article.publish_date)}
    json_data=json.dumps(数据)
    以open('data.json','w')作为输出文件:
    json.dump(数据,输出文件)
    
    否则,如果第一个文件不是具有html扩展名的文件,则尝试构建一个空字符串


    如果第一个文件是具有html扩展名的文件,但第二个文件不是,那么您将(至少)构建同一个文件两次。html肯定不是空的。感谢您的回复@sdikby 1。html肯定不是空的。2.我直接从网站上保存了网页(html)。3.在任务中,html文件将使用scrapy从web上刮取,网页将存储在本地磁盘上。我不确定这些文章是否会被下载,而且我也不太明白你所说的“有问题的”文章是什么意思。对不起,我没有找到更好的术语来描述我的意思。我所说的“有问题”是指由于
    报纸定义的某些原因(例外情况)而无法下载的文章。有一个类参数self.\u下载(我想)供您测试。或者我遗漏了一些东西,我建议不要应用allIn short,你不能解析一篇无法下载的文章。Y您还可以使用
    try..except添加is-round-artcile.download()和.parse