爬取新笔趣阁排行并保存到mysql_Python爬虫爬取新笔趣阁小说——转载

from lxml importetreeimportrequests

header={"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8","Content-Type": "application/x-www-form-urlencoded;charset=UTF-8","Accept-Encoding": "gzip, deflate","Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8","Cache-Control": "no-cache","Connection": "keep-alive","User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36"}deftrim(text_array):return "".join("".join(text_array).split())classxiaoshuo():def getText(self, url, charset="utf-8"):return str(requests.get(url, headers=header).content, charset)def __init__(self, page):

r=self.getText(page)

html=etree.HTML(r)

self.book_name= trim(html.xpath('//*[@id="info"]/h1/text()'))

self.book_author= trim(html.xpath('//*[@id="info"]/p[1]/text()'))

self.book_desc= trim(html.xpath('//*[@id="intro"]/p[position()>1]/text()'))

a= html.xpath('//*[@id="list"]/dl/dd/a')

self.chapters=[]for a1 ina:

chapter= {'name': a1.xpath('./text()')[0], 'link': 'http://www.xbiquge.la' + a1.xpath('./@href')[0]}

self.chapters.append(chapter)def __writeChapter(self, lines):

i=0for line inlines:

lines[i]= "".join(line.split()) + "\n"i+= 1with open('D:\\%s.txt' % self.book_name, 'a', encoding='utf-8') as f:

f.writelines(lines)def __findContent(self, chapter):

content= etree.HTML(self.getText(chapter['link'])).xpath('//*[@id="content"]/text()')

content.insert(0, chapter['name'] + '\n')

self.__writeChapter(content)print(chapter['name'] + '下载完成!')defdown(self):for chapter inself.chapters:

self.__findContent(chapter)if __name__ == '__main__':print("\n\t\t欢迎使用《新笔趣阁》 http://www.xbiquge.la 小说下载小工具\n\n\t\t作者:mbh \t时间:2017-05-06\n")print("*************************************************************************")#小说地址

#target_url = str(input("请输入小说目录下载地址:\n"))

page_link= 'http://www.xbiquge.la/9/9419/'xs=xiaoshuo(page_link)

xs.down()

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值