用python爬取笔趣阁小说

# -*- coding: UTF-8 -*-
from bs4 import BeautifulSoup
from urllib import request
if __name__=='__main__':
    file = open('一念永恒.txt', 'w', encoding='utf-8')
    yinianyonghen_url="http://www.biqukan.com/1_1094/"
    head={}
    head['User-Agent']="Mozilla/5.0 (Linux; Android 4.1.1; Nexus 7 Build/JRO03D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Safari/535.19"
    yinianyonghen_req=request.Request(url=yinianyonghen_url,headers=head)
    yinianyonghen_response=request.urlopen(yinianyonghen_req)
    yinianyonghen_html=yinianyonghen_response.read().decode('gbk','ignore')
    listmain_soup=BeautifulSoup(yinianyonghen_html,'lxml')
    chapters=listmain_soup.find_all('div',class_='listmain')
    download_soup=BeautifulSoup(str(chapters),'lxml')
    numbers = (len(download_soup.dl.contents) - 1) / 2 - 8
    index = 1
    begin_flag=False
    for child in download_soup.dl.children:
        if child != '\n':
            if child.string == "《一念永恒》正文卷":
                begin_flag = True
            if begin_flag == True and child.a != None:
                download_url = "http://www.biqukan.com" + child.a.get('href')
                download_name = child.string
                # print(download_name + " : " + download_url)
                download_req=request.Request(url=download_url,headers=head)
                download_response=request.urlopen(download_req)
                download_html=download_response.read().decode('gbk','ignore')
                download_name=child.string
                soup_texts=BeautifulSoup(download_html,'lxml')
                texts=soup_texts.find_all(id='content',class_='showtxt')
                soup_text=BeautifulSoup(str(texts),'lxml')
                write_flag=True
                file.write(download_name+'\n\n')
                for each in soup_text.div.text.replace('\xa0', ''):
                    if each == 'h':
                        write_flag = False
                    if write_flag == True and each != ' ':
                        file.write(each)
                    if write_flag == True and each == '\r':
                        file.write('\n')
                file.write('\n\n')
                # 打印爬取进度
                print("已下载:%.3f%%" % float(index / numbers) + '\r')
                # sys.stdout.flush()
                index += 1
    file.close()

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值