nature爬虫

就是备份一下

# -*- coding:UTF-8 -*-
from bs4 import BeautifulSoup
import requests
import re
from io import StringIO
from io import open
from pdfminer.converter import TextConverter
from pdfminer.layout import LAParams
from pdfminer.pdfinterp import PDFResourceManager, process_pdf
 
#hang=0
for i in range(10006,10011):
    def read_pdf(pdf):
        # resource manager
        rsrcmgr = PDFResourceManager()
        retstr = StringIO()
        laparams = LAParams()
        # device
        device = TextConverter(rsrcmgr, retstr, laparams=laparams)
        process_pdf(rsrcmgr, device, pdf)
        device.close()
        content = retstr.getvalue()
        retstr.close()
        jie='Received'
        x=[substr.start()for substr in re.finditer(jie,str(content))]
        con=str(content)[0:x[-1]]
        con = con.replace("\n", " ") 
        # 获取所有行

        hang=0
        target = 'https://www.nature.com/articles/nature'+'%d' %i+'#accession-codes.html'
        req = requests.get(url = target)
        html = req.text
        bf = BeautifulSoup(html)
        texts = bf.find_all("div",class_="c-article-section__content",id="Abs2-content")
        t=str(texts)
        jian=re.sub(u"\\<.*?\\>", "", t)
        jian = str(jian).replace(".", ".\n")
        jian = str(jian).replace("?", "?\n")
        jian = str(jian).replace("!", "!\n")
        for ch in jian:
            if(ch=='\n'):
                hang=hang+1
        print(jian)
        print(hang)
       
        lines = con.split(".")
        text = open("%d.txt"%i, 'w+',encoding='gb18030',errors='ignore')
        for j in range(len(lines)):
            if(len(lines[j])>20):
                if((lines[j][0].isdigit())==False and lines[j][1].isdigit()==False):
                #if(lines[j][0]>='9' and lines[j][0]<='0'):
                    hang=hang-1
                    if(hang<0):
                        text.write("\n")
            lines[j]= str(lines[j]).replace(".", ".\n")
            lines[j] = str(lines[j]).replace("!", "!\n")
            lines[j] = str(lines[j]).replace("?", ".\n")
            if(hang<0):
                text.write(lines[j])
                text.write(".")
        text.write("\n\n\nhighlight:\n")
        text.close()

        f= open("%d.txt"%i,"a+", encoding='gb18030',errors='ignore')
        f.write(str(jian))
        f.close()
        
    def _main():
        my_pdf = open('nature'+'%d' %i+'.pdf', "rb")
        read_pdf(my_pdf)
        my_pdf.close()
        
        

    if __name__ == '__main__':
        _main()
       












Azure nature是一种令人惊叹的自然景观,它象征着天蓝色的天空、碧绿的植被和清澈的水域。Azure nature可以是海洋、湖泊、河流或瀑布等各种水域景观,以及山脉、森林、草原等陆地景观。无论是在海岸线上欣赏大海波涛汹涌,还是在山巅俯瞰蔚蓝的湖泊,Azure nature都能令人陶醉于自然的美丽与宁静。 Azure nature具有独特的魅力和吸引力。首先,它给人一种宁静和放松的感觉。在蓝天和绿树的映衬下,人们可以远离城市喧嚣,享受与大自然融为一体的宁静时刻。Azure nature也给人带来一种心灵上的治愈感,让人们摆脱压力和烦恼,静心欣赏大自然的美丽。 其次,Azure nature展示了大自然的壮丽和神秘。无论是海洋的浩渺壮阔,还是山脉的层峦叠嶂,Azure nature都让人感受到大自然的伟力和无限的生命力。它也提醒人们要珍惜和保护自然资源,为了后代留下这种美丽的自然遗产。 Azure nature还提供了许多与自然互动的机会。人们可以在Azure nature中体验丰富多样的户外活动,如徒步旅行、划船、钓鱼、野营等等。这些活动不仅可以锻炼身体,更重要的是与自然和谐相处,培养人们的环保意识和对自然的尊重。 总之,Azure nature是大自然赋予我们的一份宝贵礼物。它给人们带来无尽的欢乐、宁静和启发,引导我们与自然和谐相处。在繁忙的现代生活中,我们应该抽出时间去欣赏和呵护这份珍贵的自然礼物。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值