利用python爬虫怎么对小说进行爬取

  介绍

本篇文章给大家分享的是有关利用python爬虫怎么对小说进行爬取,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

<强>代码

进口要求
  导入的时间
  从tqdm进口tqdm
  从bs4进口BeautifulSoup
  
  “““
  作者:
  杰克崔
  微信:
  https://mp.weixin.qq.com/s/OCWwRVDFNslIuKyiCVUoTA
  “““
  
  def get_content(目标):
  要求=请求。得到(url=目标)
  要求的事情。=& # 39;编码utf - 8 # 39;
  html=req.text
  男朋友=BeautifulSoup (html、& # 39; lxml # 39;)
  文本=bf.find (& # 39; div # 39; id=& # 39;内容# 39;)
  内容=texts.text.strip () .split (& # 39; \ xa0& # 39; * 4)
  返回内容
  
  if __name__==& # 39; __main__ # 39;:
  服务器=& # 39;https://www.xsbiquge.com& # 39;
  book_name=& # 39;诡秘之主. txt # 39;
  目标=& # 39;https://www.xsbiquge.com/15_15338/& # 39;
  要求=请求。得到(url=目标)
  要求的事情。=& # 39;编码utf - 8 # 39;
  html=req.text
  chapter_bs=BeautifulSoup (html、& # 39; lxml # 39;)
  章=chapter_bs.find (& # 39; div # 39; id=& # 39;列表# 39;)
  章=chapters.find_all(& # 39;一个# 39;)
  章在tqdm(章):
  chapter_name=chapter.string
  url=服务器+ chapter.get (& # 39; href # 39;)
  内容=get_content (url)
  张开(book_name & # 39;一个# 39;,编码=& # 39;utf - 8 # 39;) f:
  f.write (chapter_name)
  f.write (& # 39; \ n # 39;)
  f.write (& # 39; \ n # 39; . join(内容)
  f.write (& # 39; \ n # 39;) 

以上就是利用python爬虫怎么对小说进行爬取,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注行业资讯频道。

利用python爬虫怎么对小说进行爬取