1. import requests
    2. import os
    3. import json
    4. from lxml import etree
    5. import pandas as pd
    6. import time
    7. # 导入字体解密模块
    8. from fontTools.ttLib import TTFont
    9. '''
    10. 小说数据的爬取及其数据化分析
    11. '''
    12. results = {
    13. 'title': [], # 文章标题
    14. 'author': [], # 小说作者
    15. 'classifies': [], # 小说类别
    16. 'describes': [] # 小说的描述
    17. # 'detail': [], # 文章详情地址
    18. # 'wordcount': [] # 文字统计
    19. }
    20. class novel:
    21. def __init__(self, value):
    22. self.header = {
    23. 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4464.5 Safari/537.36',
    24. }
    25. self.page = value
    26. self.url = 'https://www.qidian.com/rank/recom?dateType=1&page={}'.format(self.page)
    27. self.foreach(value)
    28. # 页面请求
    29. def dataAnalysis(self):
    30. res = requests.get(self.url, headers=self.header).text
    31. return res
    32. # 页面解析
    33. def etreehtml(self):
    34. # 使用etree.Html方法
    35. lists = etree.HTML(self.dataAnalysis())
    36. # print(lists)
    37. content = lists.xpath('//*[@id="rank-view-list"]/div/ul/li')
    38. return content
    39. # self.dataAnalysis()
    40. # 对加密字体进行解密处理
    41. def decodeTtf(self):
    42. font = TTFont('UdygXvZa.ttf')
    43. font.saveXML('fft.xml')
    44. # 获取字体映射关系
    45. font_cmap = font['cmap'].getBestCmap()
    46. print(font_cmap)
    47. # 对数据进行遍历分析
    48. def foreach(self,value):
    49. for i in self.etreehtml():
    50. # 小说标题
    51. title = i.xpath('./div[2]/h4/a/text()')[0].replace('\n', '')
    52. results['title'].append(title)
    53. # 小说作者
    54. author = i.xpath('./div[2]/p[1]/a[1]/text()')[0].replace('\n', '')
    55. results['author'].append(author)
    56. # 小说类别
    57. classifies = i.xpath('./div[2]/p[1]/a[2]/text()')[0].replace('\n', '')
    58. results['classifies'].append(classifies)
    59. # 小说描述
    60. describes = i.xpath('./div[2]/p[2]/text()')[0].replace(" ", '').replace('\n', '').replace('\t', '').replace('\r', '')
    61. results['describes'].append(describes)
    62. print('第%s个页面已爬取完成------' % value)
    63. def buildcsv(self):
    64. # 创建表格
    65. df = pd.DataFrame(results)
    66. # 将解码方式改为ANSI 打开可以解决中文乱码的问题
    67. df.to_csv('qidian1.csv', encoding='utf8')
    68. print('------表格数据创建完成!!!')
    69. # # 文章详情页面
    70. # detail = i.xpath('./div[3]/p/a[1]/@href')[0].replace('\n', '') # 将获取到的换行数据取消,并且导入到新的数组中
    71. # results['detail'].append(detail)
    72. # # print(results)
    73. # # 进入子页面对页面的数据进行爬取遍历分别爬取
    74. # print(results['detail'])
    75. # for i in results['detail']:
    76. # url = 'https:%s' % i
    77. # res = requests.get(url, headers=self.header).text
    78. # # 对子页面进行解析
    79. # childPage = etree.HTML(res)
    80. # # numcount = childPage.xpath('/html/body/div/div[6]/div[1]/div[2]/p[3]/em')
    81. # # 字数tongj
    82. # wordcount = childPage.xpath('/html/body/div/div[6]/div[1]/div[2]/p[3]/em[1]/span/text()')
    83. # # 将字数统计加入到wordcounts当前数组中
    84. # results['wordcount'].append(wordcount)
    85. # print(results['wordcount'])
    86. if __name__ == '__main__':
    87. # 也可以使用for i range(1,6) 将所有的数据爬取出来
    88. # novel(value=input('请输入爬取的页面1-5:'))
    89. # 选择单页爬取或者多页爬取
    90. for a in range(1, 6):
    91. p = novel(str(a))
    92. time.sleep(1.5)
    93. print("------开始创建表格信息")
    94. p.buildcsv()