python 中国知网爬虫项目

python 中国知网爬虫项目

import requests
from bs4 import BeautifulSoup
import time
from urllib import parse
import bs4

search_url = 'https://link.springer.com/search/page/'
base_url = 'https://link.springer.com'
header_info={'Connection': 'close'}

value = input('请输入关键词:')
page = input('请输入页码:')
data={
    'query':value 
}
param = parse.urlencode(data)

def get_list(url,param='',page=1):
    while 1:
        try:
            res = requests.get(url=url+page,params=param,headers=header_info)
            res.encoding = res.apparent_encoding 
            return res
            
        except:
            print("连接被拒绝..")
            print("休息五秒钟zzZ")
            time.sleep(5)
            print("休息好了开始吧...")
            continue

def get_detail(url):
    while 1:
        try:
            res = requests.get(url=url,headers=header_info)
            res.encoding = res.apparent_encoding 
            return res
        except:
            print("连接被拒绝..")
            print("休息五秒钟zzZ")
            time.sleep(5)
            print("休息好了开始吧...")
            continue

def start(url,param,page):
    print('程序开始...')
    res = get_list(url,param,page)
    soup = BeautifulSoup(res.text, 'html.parser')
    ol = soup.find(name='ol', attrs={"id":"results-list","class":"content-item-list"})
    li_list = ol.find_all(name='li')
     
    for li in enumerate(li_list):
        a = li.find('a')
        # print(base_url + a.get('href'))
        detail_res = get_detail(base_url + a.get('href'))
        detail_soup = BeautifulSoup(detail_res.text, 'html.parser') 
        h1 = detail_soup.find(name='h1',attrs={'class':'c-article-title u-h1'})
        h1_p = detail_soup.find(name='h1',attrs={'class':'c-article-title u-h1'})
        h2 = detail_soup.find(name='div',attrs={'id':'Abs1-content'})
        f=open('./论文'+ str(time.time())[:10] + r'.txt',"a")
        div = detail_soup.find(name='div',attrs={'id':'Abs2-content','class':'c-article-section__content'})
        ref = detail_soup.find(name='h2',attrs={'id':'Bib1'})
        ref_div = detail_soup.find(name='div',attrs={'id':'Bib1-content','class':'c-article-section__content'})
        
        if isinstance(div,bs4.element.Tag):
            h3_list = div.find_all(name='h3')
            p_list = div.find_all(name='p')
            if h1:
                f.write(h1.get_text()+'\n\n')  
            else:
                f.write(h1_p.get_text()+'\n\n') 
            if h2:
                f.write(h2.get_text()+'\n\n')

            if len(p_list) == 1:
                f.write(p_list[0].get_text()+'\n\n') 
            else:
                for inx, val in enumerate(p_list):
                    if inx < len(h3_list):
                        f.write(h3_list[inx].get_text()+'\n') 
                    if val:
                        f.write(val.get_text()+'\n\n') 

        if ref:
            f.write('参考文献:\n')   
        if isinstance(ref_div,bs4.element.Tag):
            ref_p = ref_div.find_all(name='p')
            if len(ref_p) > 0:
                for ind, item in enumerate(ref_p):
                    f.write('【' +str(ind+1) + '】' +item.get_text()+'\n') 
        f.close()
        print('\r已抓取 -> 论文'+str(time.time())[:10])
    print('\r任务已完成')

if __name__ == '__main__':
    start(search_url,param,page)
  • 5
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值