zoukankan      html  css  js  c++  java
  • Python爬虫之利用BeautifulSoup爬取豆瓣小说(三)——将小说信息写入文件

     1 #-*-coding:utf-8-*-
     2 import urllib2
     3 from bs4 import BeautifulSoup
     4 
     5 class dbxs:
     6 
     7     def __init__(self):
     8         self.pageIndex = 0
     9         self.enable = True
    10         self.file = None
    11         self.content = []
    12         
    13         
    14     #获取html页面的内容
    15     def getPage(self, pageIndex):
    16         try:
    17             #设置代理ip
    18             enable_proxy = True
    19             proxy_handler = urllib2.ProxyHandler({'Http': '113.118.170.230:808'})
    20             null_proxy_handler = urllib2.ProxyHandler({})
    21             if enable_proxy:
    22                 opener = urllib2.build_opener(proxy_handler)
    23             else:
    24                 opener = urllib2.build_opener(null_proxy_handler)
    25             urllib2.install_opener(opener)
    26             #获得页面响应的内容
    27             url = 'https://www.douban.com/tag/%E5%B0%8F%E8%AF%B4/book' + "?start=" + str(pageIndex)
    28             #设置请求头部信息,模拟浏览器的行为
    29             my_headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:55.0)'}
    30             request =   urllib2.Request(url, headers = my_headers)
    31             response = urllib2.urlopen(request)
    32             return response.read()
    33         except urllib2.URLError, e:
    34             if hasattr(e, "code"):
    35                 print e.code
    36             if hasattr(e, "reason"):
    37                 print e.reason
    38                 return None
    39 
    40     #过滤查找这一页的小说名字,信息和评分
    41     def getContent(self, pageIndex, content):
    42         pageCode = self.getPage(pageIndex)
    43         soup = BeautifulSoup(pageCode, 'html.parser')
    44         #在获得相应的内容中找出所有标签为<dd>的内容(里面包含了我们需要的小说信息)
    45         contents = soup.find_all('dd')
    46         
    47         if contents:
    48             for item in contents:
    49                 title = item.find(class_ = 'title').string.encode('utf-8')
    50                 info = item.find(class_ = 'desc').string.strip().encode('utf-8')
    51                 rate = item.find(class_ = 'rating_nums')
    52                 #通过试验,我们发现某一页可能存在小说没有评分,如果我们不判断rate,那么可能就出现报错
    53                 if rate:
    54                     rates = rate.string.encode('utf-8')
    55                     content.append([title, info, rates])
    56                     
    57                 else:
    58                     content.append([title, info])
    59         #如果页面不包含<dd>标签,我们应该停止
    60         else:
    61             print u"所有页面已加载完"
    62             self.enable = False
    63 
    64         return content
    65         
    66             
    67 
    68     #写入文件
    69     def writeData(self, content):
    70         self.file = open("bdxs.txt", "w+")   #必须在for循环外面,不然每一次写入都会覆盖之前的数据
    71         for item in content:
    72             if len(item) == 3:
    73                 self.file.write(item[0] + "
    ")
    74                 self.file.write(item[1] + "
    ")
    75                 self.file.write(u"评分:" + item[2] + "
    
    ")
    76             else:
    77                 self.file.write(item[0] + "
    ")
    78                 self.file.write(item[1] + "
    ")
    79             self.file.write("========================================
    
    ")
    80 
    81             
    82     #创建一个开始方法
    83     def start(self):
    84         x = 1
    85         while self.enable == True:
    86             content = self.getContent(self.pageIndex, self.content)
    87             if self.enable == True:
    88                 print "正在写入第%s页..." %x
    89             self.writeData(content)
    90             self.pageIndex += 15
    91             x += 1
    92                       
    93             
    94 DBXS = dbxs()
    95 DBXS.start()

    这段代码我还没理解透彻,比如每一页的小说信息写入完成后,怎么在后面加上第几页,后期我将继续完善它。

  • 相关阅读:
    linkedLoop
    loopqueue
    expect 切换用户
    二叉树的实现
    栈的链表实现, 底层使用链表
    栈的数组实现
    RSA加密算法
    输入一个链表,反转链表后,输出链表的所有元素
    输入一个链表,输出该链表中倒数第k个结点
    ansible中include_tasks和import_tasks
  • 原文地址:https://www.cnblogs.com/my_captain/p/7470002.html
Copyright © 2011-2022 走看看