鱼C论坛

 找回密码
 立即注册
查看: 1519|回复: 6

[分享] 完美世界小说爬取程序

[复制链接]
发表于 2021-12-4 15:53:53 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
看动漫看上瘾了。刚好学到爬虫,试着简单写一个。内置库的urllib.request库用不明白,只能用第三方库写一个了。比较写得比较粗糙,不知道怎么优化输出格式,有知道的可以回复一下。有其他建议的也可说一下,大家一起学习。



                               
登录/注册后可看大图



import re
import requests

class Spider:
    headers = { #发送HTTP请求时的HEAD信息
        'Connection': 'Keep-Alive',
        'Accept': 'text/html, application/xhtml+xml, */*',
        'Accept-Language':
        'en-US,en;q=0.8,zh-Hans-CN;q=0.5,zh-Hans;q=0.3',
        'Accept-Encoding': 'gzip, deflate',
        'User-Agent':
        'Mozilla/6.1 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko'
    }
    def __init__(self,url,timeout=60):  
        self.url = url
        self.timeout = timeout
        self.titles = []
        self.data = None
        
        
   
    def getHtml(self):     #获取网页内容
        response = requests.get(self.url,timeout = self.timeout,headers=self.headers)
        response.encoding = "utf-8"
        self.html = response.text
        #print(self.html)
        
    def getContent(self):   #获取标题及网页信息
        titles = []
        self.title=re.findall(r'<div class="read_title"><h1>(.*?)</h1>',self.html)
        content_list=re.findall(r'\S&nbsp;&nbsp;&nbsp;&nbsp;(.*?)\s',self.html)
        return content_list
   
    def save_info(self,content):  #保存文件
        with open("wangmeishijie.txt","a+",encoding="utf-8") as f:
            f.write(f"{self.title[0]}\n")
            for each in content:
                f.write(f"    {each}\n")
        
def getPage(page):   
    print("正在搜索第 {} 章".format(page-643602))
    url = "https://www.soshuw.com/WanMeiShiJie/"+str(page)+".html"
    web = Spider(url)
    web.getHtml()
    content = web.getContent()
    web.save_info(content)
   
if __name__ == "__main__":
    page = 643866  #对应264章
    n = int(input("输入需要爬取的页数(从264章开始):"))
    for i in range(n):
        page += 1
        getPage(page)
    print("爬取完成")
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-12-4 15:54:06 | 显示全部楼层
  1. import re
  2. import requests

  3. class Spider:
  4.     headers = { #发送HTTP请求时的HEAD信息
  5.         'Connection': 'Keep-Alive',
  6.         'Accept': 'text/html, application/xhtml+xml, */*',
  7.         'Accept-Language':
  8.         'en-US,en;q=0.8,zh-Hans-CN;q=0.5,zh-Hans;q=0.3',
  9.         'Accept-Encoding': 'gzip, deflate',
  10.         'User-Agent':
  11.         'Mozilla/6.1 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko'
  12.     }
  13.     def __init__(self,url,timeout=60):  
  14.         self.url = url
  15.         self.timeout = timeout
  16.         self.titles = []
  17.         self.data = None
  18.         
  19.         
  20.    
  21.     def getHtml(self):     #获取网页内容
  22.         response = requests.get(self.url,timeout = self.timeout,headers=self.headers)
  23.         response.encoding = "utf-8"
  24.         self.html = response.text
  25.         #print(self.html)
  26.         
  27.     def getContent(self):   #获取标题及网页信息
  28.         titles = []
  29.         self.title=re.findall(r'<div class="read_title"><h1>(.*?)</h1>',self.html)
  30.         content_list=re.findall(r'\S&nbsp;&nbsp;&nbsp;&nbsp;(.*?)\s',self.html)
  31.         return content_list
  32.    
  33.     def save_info(self,content):  #保存文件
  34.         with open("wangmeishijie.txt","a+",encoding="utf-8") as f:
  35.             f.write(f"{self.title[0]}\n")
  36.             for each in content:
  37.                 f.write(f"    {each}\n")
  38.         
  39. def getPage(page):   
  40.     print("正在搜索第 {} 章".format(page-643602))
  41.     url = "https://www.soshuw.com/WanMeiShiJie/"+str(page)+".html"
  42.     web = Spider(url)
  43.     web.getHtml()
  44.     content = web.getContent()
  45.     web.save_info(content)
  46.    
  47. if __name__ == "__main__":
  48.     page = 643866  #对应264章
  49.     n = int(input("输入需要爬取的页数(从264章开始):"))
  50.     for i in range(n):
  51.         page += 1
  52.         getPage(page)
  53.     print("爬取完成")
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-12-4 16:11:35 | 显示全部楼层
有鱼B!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-12-5 17:56:29 | 显示全部楼层

回帖奖励 +2 鱼币

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-12-16 22:33:51 | 显示全部楼层
dlas[dl

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-12-17 09:40:37 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-12-17 15:08:45 | 显示全部楼层
bksn,牛皮
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-20 11:31

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表