沫言|嫌多(线程/进程)太慢?嫌Scrapy太麻烦?没事,异步高调走起( 二 )
发送请求:
async def scrape(self, url):async with self.semaphore:async with aiohttp.ClientSession(headers=self.header).get(url) as response:await asyncio.sleep(1)return await response.text()
注意:再次加入请求头 , 本网站对Cookies有严格的检测 。 且并不能挂IP代理访问
解析响应:
async def parse(self, html):with open('car.csv', 'a+', encoding='utf-8') as f:doc = pq(html)for message in doc('body > div.list-wrap.js-post > ul > li > a').items():# 汽车简介car_name = message('h2.t').text()# 汽车详情(年限、里程、服务)car_info = message('div.t-i').text()year = car_info[:5]mileage = car_info[6:-5]service = car_info[13:].replace('|', '')# 价格try:price = message('div.t-price > p').text()except AttributeError:price = message('em.line-through').text()car_pic = message('img').attr('src')data = http://kandian.youth.cn/index/f'{car_name}, {year},{mileage}, {service}, {price}n'logging.info(data)f.write(data)
我这里是直接一步到位了 , 解析响应 , 以及保存数据 。
运行之后即可看到类似于这样的东东
推荐阅读
- 沫言|摩托罗拉Moto G9 Plus即将上市 主要规格和价格揭秘
- 知足常乐|Java线程池原理,这一篇就够了
- 沫言|全体起立!卢本伟版的PanDownload复活了!60M/s
- 沫言|什么都是机器化,那生活还有什么意义?
- 沫言|解读数据库:深入分析MySQL中事务以及MVCC的实现原理
- 沫言|中国研发全球最大的类脑计算机!实现纯国产化:可像人脑一样思考
- 沫言|小米100wTypeC扩展坞来了,买苹果本都少不了转接口
- 沫言|买苹果本都少不了转接口,小米100wTypeC扩展坞来了
- 沫言|vivo电致变色让人不得不联想到了诺基亚5230!不可思议
- 沫言|谭松韵母亲被撞案正式开庭,嫌疑人摇头晃脑,毫无悔改太气人