Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/311.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 使用asyncio Aiofile解析大量HTML文件并在DataFrame中解析它们_Python_Pandas_Asynchronous_Python Asyncio_Python Aiofiles - Fatal编程技术网

Python 使用asyncio Aiofile解析大量HTML文件并在DataFrame中解析它们

Python 使用asyncio Aiofile解析大量HTML文件并在DataFrame中解析它们,python,pandas,asynchronous,python-asyncio,python-aiofiles,Python,Pandas,Asynchronous,Python Asyncio,Python Aiofiles,我在磁盘上有大约40000个HTML文件和一个函数,它用BeautifulSoup解析HTML,并返回每个HTML的字典。 在读取/解析过程中,我将所有字典添加到列表中,并在最后创建数据帧 它在同步模式下工作正常,但运行需要很长时间,所以我想用 当前我的代码如下所示: # Function for fetching all ad info from single page async def getFullAdSoup(soup): ... adFullFInfo = {}

我在磁盘上有大约40000个HTML文件和一个函数,它用BeautifulSoup解析HTML,并返回每个HTML的字典。 在读取/解析过程中,我将所有字典添加到列表中,并在最后创建数据帧

它在同步模式下工作正常,但运行需要很长时间,所以我想用

当前我的代码如下所示:

# Function for fetching all ad info from single page
async def getFullAdSoup(soup):
     ...
     adFullFInfo = {} # dictionary parsed from Beautifoul soup object
    return await adFullFInfo


async def main():
    adExtendedDF = pd.DataFrame()
    adExtendednfo = {}
    htmls = glob.glob("HTML_directory" + "/*.html") # Get all HTML files from directory

    htmlTasks = [] # Holds list of returned dictionaries
    for html in natsorted(htmls):
        async with aiofiles.open(html, mode='r', encoding='UTF-8', errors='strict', buffering=1) as f:
            contents = await f.read()
            htmlTasks.append(getFullAdSoup(BeautifulSoup(contents, features="lxml")))
        htmlDicts = await asyncio.gather(*htmlTasks)
    adExtendedDF = pd.DataFrame(data=htmlDicts, ignore_index=True)


if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    loop.run_until_complete(main())
我得到的错误是:

文件“C:/Users/../test.py”,第208行,在getFullAdSoup中 return await adFullFInfo TypeError:对象dict不能用于'await'表达式

我发现了类似的问题,但我无法解决。
我不知道如何将解析函数转换为异步模式,以及如何迭代调用该函数的文件。

您的错误发生是因为您等待dict,我猜您误解了,您不需要在return语句中等待它是异步的。我会像这样重构它

#从单个页面获取所有广告信息的函数
异步def getFullAdSoup(soup):
...
adFullFInfo={}#从Beautifull soup对象解析的字典
返回adFullFInfo#****1****
异步def main():
adExtendedDF=pd.DataFrame()
adExtendednfo={}
htmls=glob.glob(“HTML_目录”+“/*.HTML”)#从目录中获取所有HTML文件
htmlTasks=[]#保存返回词典的列表
对于natsorted(htmls)中的html:
与aiofiles.open异步(html,mode='r',encoding='UTF-8',errors='strict',buffering=1)为f:
contents=wait f.read()
htmlTasks.append(异步IO.create_任务(#*****2****
getFullAdSoup(BeautifulSoup(contents,features=“lxml”))
等待异步睡眠(0)#**3****
htmlDicts=等待异步IO.gather(*htmlTasks)#**4****
adExtendedDF=pd.DataFrame(数据=htmlDicts,忽略索引=True)
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
loop=asyncio.get\u event\u loop()
循环。运行\u直到完成(main())
4项变化:

  • 无需等待命令
  • 使用asyncio.create_task计划任务尽快运行
  • 休眠(0)以释放事件循环并让任务开始运行
  • 将“聚集”方法移到循环之外,以便可以一次收集所有任务,而不是一次收集一个任务

  • 2和3是可选的,但我发现它会根据您的操作产生很大的速度差异

    您的错误显然会发生,因为wait
    adFullFInfo
    是一个dict,为什么会这样?异步IO也适用于大量IO和web请求,读取和处理多个文件使用多处理我知道这一点。想法是将其转换为在asyncio中可用的内容。应用更改,缩短5个文件的文件列表。脚本没有结束。几分钟后,仍然没有任何内容写入ADEXTENDDF。