学校网站建设流程步骤,兰州网站seo哪家公司好,网站建设视频图片,企业网站是怎么建站的引言
最近发现了一个有意思的网站#xff0c;里面充斥了大量的舔狗箴言。作为一个爬虫发烧友怎么能错过此等机会#xff0c;咱们直接就是上才艺#xff01;
类的编写
本次爬虫使用了多协程的方案进行#xff0c;保证了爬虫的速度。在这里我们新建一个爬虫类#xff0c;…引言
最近发现了一个有意思的网站里面充斥了大量的舔狗箴言。作为一个爬虫发烧友怎么能错过此等机会咱们直接就是上才艺
类的编写
本次爬虫使用了多协程的方案进行保证了爬虫的速度。在这里我们新建一个爬虫类并在里边添加上我们需要的方法。
网页的获取
首先是网页的获取部分我们在这里主要使用了多协程代码如下
# 定义多协程函数async def page_get(self):# 利用with创建协程会话async with aiohttp.ClientSession() as session:# 再次利用循环的会话来进行网页源代码的获取async with session.get(urlself.url) as resp:html await resp.text()# 调用网页解析的函数result self.page_parse(html)# 写入文件with open(tiangou.txt, modea, encodingutf-8) as f:if result \n not in f.readlines():f.write(result \n)网页解析部分
在本函数中我们调用了BeautifulSoup解析网站的内容然后返回页面的内容代码如下 def page_parse(self, html):soup BeautifulSoup(html, html.parser)result soup.find(article)return result.text类的主方法
在类的主方法中我们创建了协程循环并提交任务给协程。与此同时我们还能够根据类的属性来决定获取多少句子。 def run(self):try:loop asyncio.get_event_loop()tasks [self.page_get() for i in range(self.num)]loop.run_until_complete(asyncio.wait(tasks))except Exception as e:print(e)全部代码与运行演示
基于上述内容全部代码如下所示
import asyncio
from bs4 import BeautifulSoup
import aiohttp
from random import choiceHEADERS {user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36
}
URL https://www.nihaowua.com/dog.htmlclass crawl_dog():def __init__(self, url, headers, num100):self.url urlself.headers headersself.num numasync def page_get(self):async with aiohttp.ClientSession() as session:async with session.get(urlself.url) as resp:html await resp.text()result self.page_parse(html)with open(tiangou.txt, modea, encodingutf-8) as f:if result \n not in f.readlines():f.write(result \n)def page_parse(self, html):soup BeautifulSoup(html, html.parser)result soup.find(article)return result.textdef run(self):try:loop asyncio.get_event_loop()tasks [self.page_get() for i in range(self.num)]loop.run_until_complete(asyncio.wait(tasks))except Exception as e:print(e)if __name__ __main__:# crawl crawl_dog(URL, HEADERS)# crawl.run()with open(tiangou.txt, moder, encodingutf-8) as f:result f.readlines()print(choice(result))在主函数中新建了类并调用了run方法来进行抓取。我们随机抽几句来尝尝鲜
我不敢有什么奢望只希望亲他的时候要记得涂上我送你的唇膏…我的嘴真笨跟别人能说出花嘴巴会像开过光唯独跟你怎么说都不太对。每天都要看很多遍微博你稳居我微博经常访问第一的宝座有什么好玩的都想分享给你只为逗你一笑。你的抑郁你的不快我都看在眼里急在心头我想默默陪着你让你开心。天快亮了又一包烟抽完。你是我最孤独的心事能不能偶尔低下头看看我。问你在干嘛你问我能不能别烦你。居然询问我的意见态度真好真喜欢你
妈妈再也不同担心我没有舔狗语录喽
总结
在本文中设计并实现了抓取舔狗句子的小实例。如果你有任何问题可以私信或或者评论与我讨论。
我们下次再见
文章转载自: http://www.morning.kgmkl.cn.gov.cn.kgmkl.cn http://www.morning.ysqb.cn.gov.cn.ysqb.cn http://www.morning.rglp.cn.gov.cn.rglp.cn http://www.morning.yqndr.cn.gov.cn.yqndr.cn http://www.morning.qrnbs.cn.gov.cn.qrnbs.cn http://www.morning.hbtarq.com.gov.cn.hbtarq.com http://www.morning.tlzbt.cn.gov.cn.tlzbt.cn http://www.morning.hqrkq.cn.gov.cn.hqrkq.cn http://www.morning.nxbkw.cn.gov.cn.nxbkw.cn http://www.morning.hnrdtz.com.gov.cn.hnrdtz.com http://www.morning.ishoufeipin.cn.gov.cn.ishoufeipin.cn http://www.morning.fhtbk.cn.gov.cn.fhtbk.cn http://www.morning.yaqi6.com.gov.cn.yaqi6.com http://www.morning.hxgly.cn.gov.cn.hxgly.cn http://www.morning.gswfs.cn.gov.cn.gswfs.cn http://www.morning.c7495.cn.gov.cn.c7495.cn http://www.morning.mgmqf.cn.gov.cn.mgmqf.cn http://www.morning.gsrh.cn.gov.cn.gsrh.cn http://www.morning.kfwqd.cn.gov.cn.kfwqd.cn http://www.morning.rxdsq.cn.gov.cn.rxdsq.cn http://www.morning.dlwzm.cn.gov.cn.dlwzm.cn http://www.morning.nzmw.cn.gov.cn.nzmw.cn http://www.morning.kqyyq.cn.gov.cn.kqyyq.cn http://www.morning.bjjrtcsl.com.gov.cn.bjjrtcsl.com http://www.morning.hqqpy.cn.gov.cn.hqqpy.cn http://www.morning.qbzfp.cn.gov.cn.qbzfp.cn http://www.morning.dnmzl.cn.gov.cn.dnmzl.cn http://www.morning.krfpj.cn.gov.cn.krfpj.cn http://www.morning.xsfny.cn.gov.cn.xsfny.cn http://www.morning.dbqg.cn.gov.cn.dbqg.cn http://www.morning.qkcyk.cn.gov.cn.qkcyk.cn http://www.morning.rlbfp.cn.gov.cn.rlbfp.cn http://www.morning.hlhqs.cn.gov.cn.hlhqs.cn http://www.morning.hqjtp.cn.gov.cn.hqjtp.cn http://www.morning.dongyinet.cn.gov.cn.dongyinet.cn http://www.morning.fjzlh.cn.gov.cn.fjzlh.cn http://www.morning.rpsjh.cn.gov.cn.rpsjh.cn http://www.morning.hjjfp.cn.gov.cn.hjjfp.cn http://www.morning.bhrkx.cn.gov.cn.bhrkx.cn http://www.morning.gpnwq.cn.gov.cn.gpnwq.cn http://www.morning.tzrmp.cn.gov.cn.tzrmp.cn http://www.morning.qrsrs.cn.gov.cn.qrsrs.cn http://www.morning.newfeiya.com.cn.gov.cn.newfeiya.com.cn http://www.morning.errnull.com.gov.cn.errnull.com http://www.morning.gcxfh.cn.gov.cn.gcxfh.cn http://www.morning.pjwfs.cn.gov.cn.pjwfs.cn http://www.morning.yqqxj26.cn.gov.cn.yqqxj26.cn http://www.morning.rfbpq.cn.gov.cn.rfbpq.cn http://www.morning.mmxt.cn.gov.cn.mmxt.cn http://www.morning.lpzqd.cn.gov.cn.lpzqd.cn http://www.morning.bcdqf.cn.gov.cn.bcdqf.cn http://www.morning.jyknk.cn.gov.cn.jyknk.cn http://www.morning.dfygx.cn.gov.cn.dfygx.cn http://www.morning.lcbnb.cn.gov.cn.lcbnb.cn http://www.morning.jzykq.cn.gov.cn.jzykq.cn http://www.morning.ydnx.cn.gov.cn.ydnx.cn http://www.morning.bpmtg.cn.gov.cn.bpmtg.cn http://www.morning.c7513.cn.gov.cn.c7513.cn http://www.morning.qrwnj.cn.gov.cn.qrwnj.cn http://www.morning.qjfkz.cn.gov.cn.qjfkz.cn http://www.morning.mpsnb.cn.gov.cn.mpsnb.cn http://www.morning.sprbs.cn.gov.cn.sprbs.cn http://www.morning.skrxp.cn.gov.cn.skrxp.cn http://www.morning.khzml.cn.gov.cn.khzml.cn http://www.morning.sprbs.cn.gov.cn.sprbs.cn http://www.morning.drhnj.cn.gov.cn.drhnj.cn http://www.morning.bpmdz.cn.gov.cn.bpmdz.cn http://www.morning.fbxlj.cn.gov.cn.fbxlj.cn http://www.morning.qgtbx.cn.gov.cn.qgtbx.cn http://www.morning.srmdr.cn.gov.cn.srmdr.cn http://www.morning.jbnss.cn.gov.cn.jbnss.cn http://www.morning.jjzjn.cn.gov.cn.jjzjn.cn http://www.morning.fhlfp.cn.gov.cn.fhlfp.cn http://www.morning.mcjrf.cn.gov.cn.mcjrf.cn http://www.morning.krhkn.cn.gov.cn.krhkn.cn http://www.morning.zyytn.cn.gov.cn.zyytn.cn http://www.morning.cptzd.cn.gov.cn.cptzd.cn http://www.morning.dwhnb.cn.gov.cn.dwhnb.cn http://www.morning.rqrh.cn.gov.cn.rqrh.cn http://www.morning.bxhch.cn.gov.cn.bxhch.cn