Pessoal, estou tentando entender um código que um cara do stackoverflow me ajudou. Porém retorna um erro. Ta quase!
O erro que recebo:
Traceback (most recent call last):
File "C:\Users\doug3\Projetos_PY\web_crawn\main.py", line 77, in <module>
get_results()
File "C:\Users\doug3\Projetos_PY\web_crawn\main.py", line 72, in get_results
results = event_loop.run_until_complete(*_coro)
TypeError: run_until_complete() takes 2 positional arguments but 39 were given
sys:1: RuntimeWarning: coroutine '_do_work' was never awaited
Código:
from typing import Optional, Coroutine, List
import aiohttp
from pandas import DataFrame
from pandas.errors import EmptyDataError
import pandas as pd
import asyncio
def create_df_form_file() -> Optional[DataFrame]:
try:
site_list = pd.read_csv('sites4.csv', sep=',')
df = pd.DataFrame(site_list, columns=['Site', 'Login'])
return df.assign(Site=df['Site'].map(str) + 'Login')
except EmptyDataError as e:
print(f'File Error: {e}')
return None
new_df: Optional[DataFrame] = create_df_form_file()
if not isinstance(new_df, DataFrame):
print("empty data goodbye")
exit(1)
# NOTE: Async Get Request
async def get_request(x_url: str) -> bool:
async with aiohttp.ClientSession() as session:
async with session.get(url=x_url) as result:
return result.ok
# functions to test if site needs a login and if header contains json and to include Yes or No
def needs_login(result): return result.status == 406 or result.status == 403
def is_json(result): return result.headers.get('Content-Type') == 'application/json'
async def yes_no(x): return 'yes' if await get_request(x) else 'no'
async def _do_work(site_column, _headers: dict) -> Optional[DataFrame]:
async with aiohttp.ClientSession() as session:
async with session.get(site_column['Site'], headers=_headers) as result:
if is_json(result) or needs_login(result):
# Appending Yes No
site_column['login'] = site_column['Site'].apply(yes_no)
print(site_column['Site'] + ' é Login')
print(result)
return site_column
return None
def get_results():
global new_df
headers = {'Content-Type': 'application/json'}
try:
_coro: List[Coroutine] = [_do_work(site_column, _headers=headers) for site_column in new_df['Site']]
except KeyError:
print("please insure your input file is accurate")
exit(1)
try:
event_loop = asyncio.get_event_loop()
except RuntimeError:
asyncio.set_event_loop(asyncio.new_event_loop())
event_loop = asyncio.get_event_loop()
# noinspection PyUnboundLocalVariable
results = event_loop.run_until_complete(*_coro)
print(results)
if __name__ == '__main__':
get_results()