python asyncio与aiohttp_在Python类中使用asyncio和aiohttp

本文介绍了在Python类中遇到asyncio和aiohttp使用问题的情况。当尝试在类中使用这些库时,脚本会意外锁定。作者展示了Fetch类的代码,包括初始化、设置请求头、代理以及异步请求的方法。同时,还给出了在Main.py中调用此类的方式。问题可能在于类的结构或异步操作的管理,需要进一步排查和调整以确保正确执行。
摘要由CSDN通过智能技术生成

我一直试图找出如何在类中使用asyncio和aiohttp . 如果我只是尝试运行没有Class的脚本(只是按原样使用函数),一切正常 . 只要我将所有函数带入一个类并尝试使用Main.py中的类,脚本就会锁定而不会出现任何错误 . 不确定从哪里开始,我猜我必须以不同方式设置我的 class 才能工作 . 如果有人知道为什么这不起作用,如果你分享我做错了,我将不胜感激 . 感谢您的时间 .

Fetch.py

import asyncio

from aiohttp import ClientSession

class Fetch:

def __init__(self, proxy=None):

self.proxy = proxy

self.headers = {'user-agent': 'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'}

def set_headers(self, headers):

if not headers:

headers = self.headers

return headers

def set_proxy(self, proxy):

if proxy:

p = proxy

else:

p = self.proxy

return "http://{}".format(p)

async def get_fetch(self, session, url, headers=None, proxy=None, params=None, timeout=9):

array = []

while True:

try:

async with session.get(url, headers=self.set_headers(headers), proxy=self.set_proxy(proxy), params=params, timeout=timeout) as r:

print (r.status)

if r.status == 200:

obj = await r.read()

array.append(obj)

break

except:

pass

return array

async def get_bound(self, sem, session, url):

async with sem:

array = await self.get_fetch(session, url)

return array

async def get_run(self, urls, semaphores=400):

tasks = []

sem = asyncio.Semaphore(semaphores)

async with ClientSession() as session:

for url in urls:

task = asyncio.ensure_future(self.get_bound(sem, session, url))

tasks.append(task)

responses = await asyncio.gather(*tasks)

return responses

def get(self, urls):

loop = asyncio.get_event_loop()

future = asyncio.ensure_future(self.get_run(urls))

array = loop.run_until_complete(future)

loop.close()

return [ent for sublist in array for ent in sublist]

Main.py

from Browser import Fetch

from bs4 import BeautifulSoup

proxy = 'xxx.xxx.xxx.xxx:xxxxx'

fetch = Fetch(proxy)

if __name__ == '__main__':

urls = ['http://ip4.me','http://ip4.me','http://ip4.me']

array = fetch.get(urls)

for obj in array:

soup = BeautifulSoup(obj, 'html.parser')

for ip in soup.select('tr + tr td font'):

print(ip.get_text())

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值