按照卷号和期号抓取Geophysics论文
论文词条的输出格式如下:1
作者,出版年份,论文标题,Geophysics,卷号(期号),页码,doi.
论文抓取的函数如下(使用到了bs4和requests模块)1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61def get_seg_paper_lists(volume_no, issue_no, write_path):
'''
volume_no: Geophysics卷号
issue_no: Geophysics期号
write_path:需要将文件写入的路径
'''
# 需要抓取论文信息的页面
# 例(2018年3-4月第83卷第2期):https://library.seg.org/toc/gpysa7/83/2
url = 'https://library.seg.org/toc/gpysa7/'+ '%02d'%(volume_no) + '/%d'%(issue_no)
html = requests.get(url)
html.encoding = 'utf-8'
soup = BeautifulSoup(html.text, "html.parser")
# 解析html文本
## 每一篇论文都是一个titled_issues, 并且有的标签还包含了章节名titled_issues__title to-section
issues = soup.find_all('div', 'titled_issues')
# 创建空字典,将章节名作为key,将章节下的包含文章词条的列表作为value
contents = dict()
# 循环遍历该volume-issue下的论文
for issue in issues:
# 尝试获取章节名,如果章节名不是字典的键,则为该章节名添加空列表
to_section = issue.find('h4', 'titled_issues__title to-section')
if to_section:
Section = to_section.string
if Section not in contents.keys():
contents[Section]=[]
# 获取页码
pages = get_pages(issue)
# 获取作者列表
authors = get_authors(issue)
# 获取论文标题
title = issue.find('div', 'issue-item__title').string
# 获取论文doi
doi = issue.find('div', 'issue-item__doi').string
# 若论文作者列表为空则设置为字符串‘None’
authors = 'None' if authors is None else authors
# 如果作者、标题和doi均不为空,则将论文信息整理为指定格式
if all([authors, title, doi]):
ref = ref_type(authors,
title.title(),
volume2year(volume_no),
volume_no,
issue_no,
pages,
doi.title())
# 将论文词条ref添加到其对应的章节Section中
contents[Section].append(ref)
# 将章节和论文内容按照每卷每期写入文件
# 章节1:
# 论文1
# 论文2
# 章节2:
# 论文1
# ····
with open(write_path, 'w', encoding='utf-8') as f:
for key, value in zip(contents.keys(), contents.values()):
f.write(key+':\n')
for title in value:
if title:
f.write('\t'+title+'\n')
f.write('\n')
print('Lists have been written into %s'%(write_path))
子函数如下:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59# 词条格式
def ref_type(author_list, title, year, volume, issue, pages, doi):
'''
author_list: 作者列表
title :标题
year : 论文出版年份
volume :论文出版卷号
issue :论文出版期号
pages : 页码
doi :doi
'''
ref = ', '.join([author_list,
'%d'%(year),
title,
'Geophysics',
'%d(%d)'%(volume, issue),
pages])
ref = ref + '. doi: %s.'%(doi.lower())
return ref
# 根据作者的数量输出合适的格式
def combine_authors(author_list):
'''
author_list: 列表,包含作者的名字
'''
# 只有一个作者
if len(author_list)==1:
return author_list[0]
# 两个作者,中间用and连接
if len(author_list)==2:
return ' and '.join(author_list)
# 大于两个作者,出最后一个作者用and连接外,其他作者用,
if len(author_list)>2:
new_list = ', '.join(author_list[0:-1])
new_list += ' and '+author_list[-1]
return new_list
# 从html中抓取作者信息
def get_authors(issue):
'''
issue: <titled_issues>每个论文的节点标签。
'''
authors = issue.find('div', 'issue-item__authors')
author_list = [d.string.title() for d in authors.find_all('a')]
author_list = combine_authors(author_list)
return author_list
# 从html中抓取页码信息
def get_pages(issue):
'''
issue: <titled_issues>每个论文的节点标签。
'''
pages = issue.find('ul', 'rlist--inline separator toc-item__detail').find('li').get_text(strip=True)
pages = pages.replace('Pages:', '').replace(' ', '')
return pages
# 将卷转换为年
def volume2year(volume):
'''
volume: 卷号。2017年为第82卷。
'''
return volume + 2017 - 82
以上即为如何利用bs4从html中解析文献信息。
1 | # 脚本执行如下命令即可将第82卷第1期的文章列表下载到文件V82I1.txt中 |