码迷,mamicode.com
首页 > 其他好文 > 详细

网络爬虫练习之网络小说

时间:2019-10-31 23:44:28      阅读:212      评论:0      收藏:0      [点我收藏+]

标签:chapter   request   练习   class   int   with open   end   ext   div   

 1 import requests
 2 import bs4
 3 
 4 #获取网页代码
 5 def gethtml(url):
 6     try:
 7         response = requests.get(url)
 8         response.raise_for_status()
 9         response.encoding = response.apparent_encoding
10         return response.text
11     except:
12         return "禁止爬取本网站"
13 
14 #获取每一页中的文字
15 def chapters(url,name):
16     html = gethtml("http://www.bjkgjlu.com"+url)
17     soup = bs4.BeautifulSoup(html,html.parser)
18     for i in soup.find_all("div",attrs={"class":"chapter_content"}):
19         with open(name+".txt","wb") as f:
20             f.write(i.text.split("&lt")[0].encode("utf-8"))
21             print(name+"爬取结束,并存入文件")
22 
23 if __name__=="__main__":
24     url = "http://www.bjkgjlu.com/303618kyi/catalog"
25     chapter_name_list = []
26     chapter_url_list = []
27     html =gethtml(url)
28     soup = bs4.BeautifulSoup(html, "html.parser")
29 
30     for i in soup.findAll("div", attrs={"class": "col-xs-120 col-sm-60 col-md-40 col-lg-30"}):
31         for j in i.children:
32             chapter_name_list.append(j.text)
33             chapter_url_list .append(j.get("href"))
34     print(chapter_name_list )
35     for j in range(len(chapter_name_list)):
36         chapters(chapter_url_list[j],chapter_name_list[j] )

 

网络爬虫练习之网络小说

标签:chapter   request   练习   class   int   with open   end   ext   div   

原文地址:https://www.cnblogs.com/lsyb-python/p/11774319.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!