Asynchrone HTML-Analyse mit Beautifulsoup4 in PythonPython

Python-Programme
Anonymous
 Asynchrone HTML-Analyse mit Beautifulsoup4 in Python

Post by Anonymous »

Ich erstelle ein Python-Web-Scraper-Skript. Ich sollte dies mit Asyncio tun. Für asynchrone HTTP-Anfragen verwende ich also AioHTTP.

Es ist in Ordnung, aber wenn ich versuche, eine nicht blockierende App zu erstellen (warten), blockiert beautifulsoup4 die Anwendung (weil beautifulsoup4 Async nicht unterstützt)

Das habe ich versucht.

Code: Select all

import asyncio, aiohttp
from bs4 import BeautifulSoup

async def extractLinks(html):
soup = BeautifulSoup(html, 'html.parser')
return soup.select(".c-pro-box__title a")

async def getHtml(session, url):
async with session.get(url) as response:
return await response.text()

async def loadPage(url):
async with aiohttp.ClientSession() as session:
html = await getHtml(session, url)
links = await extractLinks(html)
return links

loop = asyncio.get_event_loop()
loop.run_until_complete(loadPage())
ExtractLinks() blockiert den Programmfluss.

Ist es also möglich, ihn nicht blockierend zu machen? Oder gibt es außer beautifulsoup4 eine Bibliothek, die Async so gut wie möglich unterstützt?

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post