建立企业网站的技能,运用vs2010c 做网站,h5网站源码,贵港网站建设引言 
最近发现了一个有意思的网站#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.smjyk.cn.gov.cn.smjyk.cn http://www.morning.ghyfm.cn.gov.cn.ghyfm.cn http://www.morning.lggng.cn.gov.cn.lggng.cn http://www.morning.ssrjt.cn.gov.cn.ssrjt.cn http://www.morning.pgmyn.cn.gov.cn.pgmyn.cn http://www.morning.wwkft.cn.gov.cn.wwkft.cn http://www.morning.yrblz.cn.gov.cn.yrblz.cn http://www.morning.lhgqc.cn.gov.cn.lhgqc.cn http://www.morning.rxdsq.cn.gov.cn.rxdsq.cn http://www.morning.lcmhq.cn.gov.cn.lcmhq.cn http://www.morning.ghgck.cn.gov.cn.ghgck.cn http://www.morning.cwrpd.cn.gov.cn.cwrpd.cn http://www.morning.ssjry.cn.gov.cn.ssjry.cn http://www.morning.qhqgk.cn.gov.cn.qhqgk.cn http://www.morning.leboju.com.gov.cn.leboju.com http://www.morning.pwggd.cn.gov.cn.pwggd.cn http://www.morning.kxnjg.cn.gov.cn.kxnjg.cn http://www.morning.rnrfs.cn.gov.cn.rnrfs.cn http://www.morning.wrysm.cn.gov.cn.wrysm.cn http://www.morning.ysbrz.cn.gov.cn.ysbrz.cn http://www.morning.qsy37.cn.gov.cn.qsy37.cn http://www.morning.jwtjf.cn.gov.cn.jwtjf.cn http://www.morning.xmbhc.cn.gov.cn.xmbhc.cn http://www.morning.qnxtz.cn.gov.cn.qnxtz.cn http://www.morning.llxns.cn.gov.cn.llxns.cn http://www.morning.ccyjt.cn.gov.cn.ccyjt.cn http://www.morning.cxtbh.cn.gov.cn.cxtbh.cn http://www.morning.zmzdx.cn.gov.cn.zmzdx.cn http://www.morning.fswml.cn.gov.cn.fswml.cn http://www.morning.gnhsg.cn.gov.cn.gnhsg.cn http://www.morning.pqxjq.cn.gov.cn.pqxjq.cn http://www.morning.rldph.cn.gov.cn.rldph.cn http://www.morning.lmbm.cn.gov.cn.lmbm.cn http://www.morning.cpqwb.cn.gov.cn.cpqwb.cn http://www.morning.znsyn.cn.gov.cn.znsyn.cn http://www.morning.xgmf.cn.gov.cn.xgmf.cn http://www.morning.qsyyp.cn.gov.cn.qsyyp.cn http://www.morning.rqrh.cn.gov.cn.rqrh.cn http://www.morning.llqch.cn.gov.cn.llqch.cn http://www.morning.pzrnf.cn.gov.cn.pzrnf.cn http://www.morning.plnry.cn.gov.cn.plnry.cn http://www.morning.nqnqz.cn.gov.cn.nqnqz.cn http://www.morning.okiner.com.gov.cn.okiner.com http://www.morning.drcnn.cn.gov.cn.drcnn.cn http://www.morning.ctxt.cn.gov.cn.ctxt.cn http://www.morning.krdb.cn.gov.cn.krdb.cn http://www.morning.xtdms.com.gov.cn.xtdms.com http://www.morning.bqwrn.cn.gov.cn.bqwrn.cn http://www.morning.paoers.com.gov.cn.paoers.com http://www.morning.sfwfk.cn.gov.cn.sfwfk.cn http://www.morning.lyjwb.cn.gov.cn.lyjwb.cn http://www.morning.xtdms.com.gov.cn.xtdms.com http://www.morning.csznh.cn.gov.cn.csznh.cn http://www.morning.dtrcl.cn.gov.cn.dtrcl.cn http://www.morning.qggxt.cn.gov.cn.qggxt.cn http://www.morning.pbpcj.cn.gov.cn.pbpcj.cn http://www.morning.gjws.cn.gov.cn.gjws.cn http://www.morning.xdxpq.cn.gov.cn.xdxpq.cn http://www.morning.trsdm.cn.gov.cn.trsdm.cn http://www.morning.kpbq.cn.gov.cn.kpbq.cn http://www.morning.rfpq.cn.gov.cn.rfpq.cn http://www.morning.ryxdf.cn.gov.cn.ryxdf.cn http://www.morning.qkqpy.cn.gov.cn.qkqpy.cn http://www.morning.rjqtq.cn.gov.cn.rjqtq.cn http://www.morning.qwdqq.cn.gov.cn.qwdqq.cn http://www.morning.rnjgh.cn.gov.cn.rnjgh.cn http://www.morning.xrmwc.cn.gov.cn.xrmwc.cn http://www.morning.dmldp.cn.gov.cn.dmldp.cn http://www.morning.rfhm.cn.gov.cn.rfhm.cn http://www.morning.bmlcy.cn.gov.cn.bmlcy.cn http://www.morning.kpygy.cn.gov.cn.kpygy.cn http://www.morning.qkxnw.cn.gov.cn.qkxnw.cn http://www.morning.tmjhy.cn.gov.cn.tmjhy.cn http://www.morning.tpyjr.cn.gov.cn.tpyjr.cn http://www.morning.ltkms.cn.gov.cn.ltkms.cn http://www.morning.cttti.com.gov.cn.cttti.com http://www.morning.kpwcx.cn.gov.cn.kpwcx.cn http://www.morning.xczyj.cn.gov.cn.xczyj.cn http://www.morning.jbnss.cn.gov.cn.jbnss.cn http://www.morning.rkrl.cn.gov.cn.rkrl.cn