关键词文章采集源码(爬取了“新闻传播”主题下的文章标题及发表时间 )

优采云 发布时间: 2021-11-22 01:16

  关键词文章采集源码(爬取了“新闻传播”主题下的文章标题及发表时间

)

  前几天帮朋友做了一个知网爬虫,爬取了“新闻传播”话题下文章的标题和发表时间;拖了2天写完,还是太虚弱了。个人觉得这是一个很好的爬虫项目,适合动手实践,所以写了主要步骤,把代码放到了我的github上。有需要的朋友可以看看或指点我改进。我的github-知网爬虫的github链接。

  1. 知网爬虫的爬虫首先要找到一个合适的知网爬虫入口,建议从这个链接进入知网入口;

  2. 输入你要抓取的话题,搜索,观察网址变化。你此时看到的网址没有长后缀,继续往下看;

  

  3. 接下来我们翻页看看URL的变化。我们发现每页只有15个文章标题,而且只有15条信息是异步加载的,所以我们构造了pagenext()函数进行翻页;

  

  4. 打开开发者工具,搜索标题文字的标签文章,观察标签中的文字,发现是分开的,所以只能找到上层标签或上层所在两个title是位于Tags,通过BeautifulSoup和get_text()选择提取文本,这里我选择了h3标签;

  

  5. 接下来我们需要选择每篇文章的发表日期文章,这需要我们点击进入每篇文章文章选择日期,通过BS选择字体标签,找到color="#0080ff"标签,提取文字,可以确定发表时间;

  

  6. 但是在爬取过程中,我们发现每个文章的URL都不一样,甚至有些URL根本没有文章。于是我观察了url的组成,发现一共有三种,只能使用两种类型的url,所以我用正则表达式来匹配可以使用的标签,然后请求提取<的发布时间@文章;

  if re.match(r"""http://youxian.cnki.com.cn/yxdetail.aspx\?filename=[0-9a-zA-Z]+&dbname=[a-zA-Z]+""",text_url) or re.match(r'http://www.cnki.com.cn/Article/[a-zA-Z]+-[0-9a-zA-Z-]+.htm',text_url):

# print(text.find('a')['href'])

text_date = datespider(text_url)

  7. 保留抓到的数据,然后写入excel,完成对zhinet的爬取;

  粘贴源代码如下:

  import requests

from bs4 import BeautifulSoup as bs

import time

import xlwt

import openpyxl

import re

def pagenext():

base_url = 'http://search.cnki.com.cn/search.aspx?q=%E6%96%B0%E9%97%BB%E4%BC%A0%E6%92%AD&rank=relevant&cluster=Type&val=I141&p='

L = range(0,840) # 最尾巴的数不计入

All_Page = []

for i in L[::15]:

next_url = base_url+str(i)

# print(next_url)

print("第 ",i/15+1," 页的数据")

page_text = spider(next_url)

time.sleep(10)

for page in page_text:

All_Page.append(page)

# print(All_Page)

write_excel('xlsx论文筛选.xlsx','info',All_Page)

def datespider(date_url):

# 因为跳转的链接类型不一样,所以我们要判断这两种链接是哪一种并且选择不一样的解析find方法

response_try = requests.get(date_url,{'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36'})

# print(response_try.text)

response_tree = bs(response_try.text,'html.parser')

# 根据两个不同的链接返回不一样的值

if re.match(r'http://www.cnki.com.cn/Article/[0-9a-zA-Z\_]+',date_url):

res_date = response_tree.find("font",{"color":"#0080ff"})

if res_date == None:

response_date = None

else:

response_date = res_date.get_text().replace('\r','').replace('\n','')

else:

response_date = response_tree.find("title").get_text()[-8:]

return response_date

def write_excel(path,sheet_name,text_info):

index = len(text_info)

workbook = openpyxl.Workbook()

sheet = workbook.active

sheet.title = sheet_name

for i in range(0,index):

for j in range(len(text_info[i])):

sheet.cell(row= i+1,column = j+1,value = str(text_info[i][j]))

workbook.save(path)

print("xlsx格式表格写入数据成功!")

def spider(url):

response = requests.get(url,{'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36'})

res = response.content

html = str(res,'utf-8')

html_tree = bs(html,'lxml')

# 找打h3标签下的内容

html_text = html_tree.find_all("h3")

All_text = []

# 隔一个才是文章的标题

for text in html_text[1:-2:]:

one_text = []

text_title = text.get_text().replace('\xa0','').replace('\n','')# 得到论文的标题

# print(text.get_text())

text_url = text.find('a')['href'] # 选取了当前文章的链接

# 用正则表达式匹配我们需要的链接

if re.match(r"""http://youxian.cnki.com.cn/yxdetail.aspx\?filename=[0-9a-zA-Z]+&dbname=[a-zA-Z]+""",text_url) or re.match(r'http://www.cnki.com.cn/Article/[a-zA-Z]+-[0-9a-zA-Z-]+.htm',text_url):

# print(text.find('a')['href'])

text_date = datespider(text_url)

one_text.append(text.get_text().replace('\xa0','').replace('\n','')) # text.get_text是得到文章的标题

if text_date == None:

one_text.append(None)

else:

if int(text_date[:4])>=2014:

one_text.append(text_date.replace('\t','').replace('\r','').replace('\n','').replace(' ',''))

else:

continue

All_text.append(one_text)

# print(text.find('a')['href'])

# print(All_text)

return All_text

# write_excel(All_text)

if __name__ =='__main__':

pagenext()

0 个评论

要回复文章请先登录注册


官方客服QQ群

微信人工客服

QQ人工客服


线