qq_56038802 2022-04-04 16:05 采纳率: 25%
浏览 58
已结题

异步爬取ip时 try语句出现问题

我打印的所有的ip全是错误的
但是我自己单独使用不出错 应该是verify函数出错了 但是不知道怎么改 感觉timeout还没判断就执行到except去了

要没有人能够看出问题 下面是代码

import json
import requests
from bs4 import BeautifulSoup
import aiohttp
import aiofiles
import asyncore
import json
from lxml import etree


async def get_ip(url):
    async with aiohttp.ClientSession() as session:
        async with session.get(url) as f:
            a = await f.text()
            bsl = BeautifulSoup(a,'html.parser')
            bss = bsl.find('table',width="100%").select('tr')[1:]
            for list in bss:
                ip = list.select('tr td')[0].text
                port = list.select('tr td')[1].text
                proxies={
                    f'http':f'http://{ip}:{port}',
                    f'https':f'https://{ip}:{port}'
                }
                asyncio.gather(verify(proxies))


async def verify(proxies):
    async with aiohttp.ClientSession() as session:
        try:
            f = await session.get('https://www.baidu.com',proxies=proxies,async_timeout = 3)
            print('可用代理:{}'.format(proxies))
            await write_json(proxies)
        except:
            print('不可用的:{}'.format(proxies))



async def write_json(proxies):
    async with aiofiles.open('ip处理池.json','a') as f:
        await json.dump(proxies,f)


async def rea_json():
    async with aiofiles.open('ip处理池.json','r')as f:
        for i in f.readlines():
            content = json.loads(i.strip())
            print(content)


async def main():
    tasks = []
    for i in range(100):
        url = f'http://www.66ip.cn/{i}.html'
        tasks.append(asyncio.create_task(get_ip(url)))
    await asyncio.wait(tasks)



if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    loop.run_until_complete(main())


  • 写回答

3条回答 默认 最新

  • CSDN专家-showbo 2022-04-04 16:17
    关注

    变量和函数名冲突了,换个变量名

    img

    
                text = await f.text()##########
                bsl = BeautifulSoup(text ,'html.parser')######
    
    

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 4月12日
  • 已采纳回答 4月4日
  • 修改了问题 4月4日
  • 修改了问题 4月4日
  • 展开全部

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度