国外的大学图书馆也像国内的一样吗?用Python脚本抓取期刊的主题标题!

简介: 国外的大学图书馆也像国内的一样吗?用Python脚本抓取期刊的主题标题!
catalogs = {
#‘catalog name’ : {
‘base_url’ : beginning part of URL from ‘http://’ to before first ‘/’,
‘search_url’ : URL for online catalog search without base URL including ‘/’;
make sure that ‘{0}’ is in the proper place for the query of ISSN,
‘search_title’ : CSS selector for parent element of anchor
containing the journal title on search results in HTML,
‘bib_record’ : CSS selector for record metadata on catalog item’s HTML page,
‘bib_title’ : CSS selector for parent element of anchor containing the journal title,
‘bib_subjects’ : HTML selector for specific table element where text begins with
“Topics”, “Subject” in catalog item’s HTML page in context of bib_record
‘worldcat’ : {
‘base_url’ : “https://www.worldcat.org”,
‘search_url’ : “/search?qt=worldcat_org_all&q={0}”,
‘search_title’ : “.result.details .name”,
‘bib_record’ : “div#bibdata”,
‘bib_title’ : “div#bibdata h1.title”,
‘bib_subjects’ : “th”
},
‘carli_i-share’ : {
‘base_url’ : “https://vufind.carli.illinois.edu”,
‘search_url’ : “/all/vf-sie/Search/Home?lookfor={0}&type=isn&start_over=0&submit=Find&search=new”,
‘search_title’ : “.result .resultitem”,
‘bib_record’ : “.record table.citation”,
‘bib_title’ : “.record h1”,
‘bib_subjects’ : “th”
},
‘mobius’ : {
‘base_url’ : ‘https://searchmobius.org’,
‘search_url’ : “/iii/encore/search/C__S{0}%20__Orightresult__U?lang=eng&suite=cobalt”,
‘search_title’ : “.dpBibTitle .title”,
‘bib_record’ : “table#bibInfoDetails”,
‘bib_title’ : “div#bibTitle”,
‘bib_subjects’ : “td”
}
}
Obtain the right parameters for specific catalog systems
Input: catalog name: ‘worldcat’, ‘carli i-share’, ‘mobius’
Output: dictionary of catalog parameters
def get_catalog_params(catalog_key):
try:
return catalogs[catalog_key]
except:
print(‘Error - unknown catalog %s’ % catalog_key)
Search catalog for item by ISSN
Input: ISSN, catalog parameters
Output: full URL for catalog item
def search_catalog (issn, p = catalogs[‘carli_i-share’]):
title_url = None
catalog url for searching by issn
url = p[‘base_url’] + p[‘search_url’].format(issn)
u = urlopen (url)
try:
html = u.read().decode(‘utf-8’)
finally:
u.close()
try:
soup = BeautifulSoup (html, features=“html.parser”)
title = soup.select(p[‘search_title’])[0]
title_url = title.find(“a”)[‘href’]
except:
print(‘Error - unable to search catalog by ISSN’)
return title_url
return p[‘base_url’] + title_url
Scrape catalog item URL for metadata
Input: full URL, catalog parameters
Output: dictionary of catalog item metadata,
including title and subjects
def scrape_catalog_item(url, p = catalogs[‘carli_i-share’]):
result = {‘title’:None, ‘subjects’:None}
u = urlopen (url)
try:
html = u.read().decode(‘utf-8’)
finally:
u.close()
try:
soup = BeautifulSoup (html, features=“html.parser”)

title

try:
title = soup.select_one(p[‘bib_title’]).contents[0].strip()

save title to result dictionary

result[“title”] = title
except:
print(‘Error - unable to scrape title from url’)

subjects

try:
record = soup.select_one(p[‘bib_record’])
subject = record.find_all(p[‘bib_subjects’], string=re.compile(“(Subjects*|Topics*)”))[0]
subject_header_row = subject.parent
subject_anchors = subject_header_row.find_all(“a”)
subjects = []
for anchor in subject_anchors:
subjects.append(anchor.string.strip())

save subjects to result dictionary

result[“subjects”] = subjects
except:
print(‘Error - unable to scrape subjects from url’)
except:
print(‘Error - unable to scrape url’)
return result


Search for catalog item and process metadata from item’s HTML page
Input: ISSN, catalog paramters
Output: dictionary of values: issn, catalog url, title, subjects
def get_issn_data(issn, p = catalogs[‘carli_i-share’]):
results = {‘issn’:issn, ‘url’:None, ‘title’:None, ‘subjects’:None}
time.sleep(time_delay)
url = search_catalog(issn, params)
results[‘url’] = url
if url: # only parse metadata for valid URL
time.sleep(time_delay)
item_data = scrape_catalog_item(url, params)
results[‘title’] = item_data[‘title’]
if item_data[‘subjects’] is not None:
results[‘subjects’] = ‘,’.join(item_data[‘subjects’]).replace(‘, -’, ’ - ')
return results

main loop to parse all journals

time_delay = 0.5 # time delay in seconds to prevent Denial of Service (DoS)
try:

setup arguments for command line

args = sys.argv[1:]
parser = argparse.ArgumentParser(description=‘Scrape out metadata from online catalogs for an ISSN’)
parser.add_argument(‘catalog’, type=str, choices=(‘worldcat’, ‘carli_i-share’, ‘mobius’), help=‘Catalog name’)
parser.add_argument(‘-b’, ‘–batch’, nargs=1, metavar=(‘Input CSV’), help=‘Run in batch mode - processing multiple ISSNs’)
parser.add_argument(‘-s’, ‘–single’, nargs=1, metavar=(‘ISSN’), help=‘Run for single ISSN’)
args = parser.parse_args()
params = get_catalog_params(args.catalog) # catalog parameters

single ISSN

if args.single is not None:
issn = args.single[0]
r = get_issn_data(issn, params)
print(‘ISSN: {0}\r\nURL: {1}\r\nTitle: {2}\r\nSubjects: {3}’.format(r[‘issn’], r[‘url’], r[‘title’], r[‘subjects’]))

multiple ISSNs

elif args.batch is not None:
input_filename = args.batch[0]
output_filename = ‘batch_output_{0}.csv’.format(args.catalog) # put name of catalog at end of output file
with open(input_filename, mode=‘r’) as csv_input, open(output_filename, mode=‘w’, newline=‘’, encoding=‘utf-8’) as csv_output:
read_in = csv.reader(csv_input, delimiter=‘,’)
write_out = csv.writer(csv_output, delimiter=‘,’, quotechar=‘"’, quoting=csv.QUOTE_MINIMAL)
write_out.writerow([‘ISSN’, ‘URL’, ‘Title’, ‘Subjects’]) # write out headers to output file
total_rows = sum(1 for row in read_in) # read all rows to get total
csv_input.seek(0) # move back to beginning of file
read_in = csv.reader(csv_input, delimiter=‘,’) # reload csv reader object
for row in tqdm(read_in, total=total_rows): # tqdm is progress bar

each row is an ISSN

issn = row[0]
r = get_issn_data(issn, params)
write_out.writerow([r[‘issn’], r[‘url’], r[‘title’], r[‘subjects’]])

文末有福利领取哦~

👉一、Python所有方向的学习路线

Python所有方向的技术点做的整理,形成各个领域的知识点汇总,它的用处就在于,你可以按照上面的知识点去找对应的学习资源,保证自己学得较为全面。

👉二、Python必备开发工具

👉三、Python视频合集

观看零基础学习视频,看视频学习是最快捷也是最有效果的方式,跟着视频中老师的思路,从基础到深入,还是很容易入门的。

👉 四、实战案例

光学理论是没用的,要学会跟着一起敲,要动手实操,才能将自己的所学运用到实际当中去,这时候可以搞点实战案例来学习。(文末领读者福利)

👉五、Python练习题

检查学习结果。

👉六、面试资料

我们学习Python必然是为了找到高薪的工作,下面这些面试题是来自阿里、腾讯、字节等一线互联网大厂最新的面试资料,并且有阿里大佬给出了权威的解答,刷完这一套面试资料相信大家都能找到满意的工作。

👉因篇幅有限,仅展示部分资料,这份完整版的Python全套学习资料已经上传


相关文章
|
2月前
|
数据采集 Web App开发 JavaScript
基于Selenium的Python爬虫抓取动态App图片
基于Selenium的Python爬虫抓取动态App图片
248 68
|
2月前
|
数据采集 运维 API
把Postman调试脚本秒变Python采集代码的三大技巧
本文介绍了如何借助 Postman 调试工具快速生成 Python 爬虫代码,并结合爬虫代理实现高效数据采集。文章通过“跨界混搭”结构,先讲解 Postman 的 API 调试功能,再映射到 Python 爬虫技术,重点分享三大技巧:利用 Postman 生成请求骨架、通过 Session 管理 Cookie 和 User-Agent,以及集成代理 IP 提升稳定性。以票务信息采集为例,展示完整实现流程,探讨其在抗封锁、团队协作等方面的价值,帮助开发者快速构建生产级爬虫代码。
103 1
把Postman调试脚本秒变Python采集代码的三大技巧
|
2月前
|
供应链 API 开发者
1688 商品数据接口终极指南:Python 开发者如何高效获取标题 / 价格 / 销量数据(附调试工具推荐)
1688商品列表API是阿里巴巴开放平台提供的服务,允许开发者通过API获取1688平台的商品信息(标题、价格、销量等)。适用于电商选品、比价工具、供应链管理等场景。使用时需构造请求URL,携带参数(如q、start_price、end_price等),发送HTTP请求并解析返回的JSON/XML数据。示例代码展示了如何用Python调用该API获取商品列表。
130 18
|
2月前
|
数据采集 存储 前端开发
Python爬虫自动化:批量抓取网页中的A链接
Python爬虫自动化:批量抓取网页中的A链接
|
2月前
|
JSON API 数据格式
手把手教你抓取京东商品评论:API 接口解析与 Python 实战
京东商品评论蕴含用户对产品质量、体验和服务的真实反馈,分析这些数据有助于企业优化产品和满足用户需求。由于京东未提供官方API,需通过逆向工程获取评论数据。其主要接口为“商品评论列表接口”,支持按商品ID、评分、排序方式等参数获取评论,返回JSON格式数据,包含评论列表、摘要(如好评率)及热门标签等信息。
|
3月前
|
数据采集 JSON API
Python 实战:用 API 接口批量抓取小红书笔记评论,解锁数据采集新姿势
小红书作为社交电商的重要平台,其笔记评论蕴含丰富市场洞察与用户反馈。本文介绍的小红书笔记评论API,可获取指定笔记的评论详情(如内容、点赞数等),支持分页与身份认证。开发者可通过HTTP请求提取数据,以JSON格式返回。附Python调用示例代码,帮助快速上手分析用户互动数据,优化品牌策略与用户体验。
|
3月前
|
数据采集 存储 缓存
Python爬虫与代理IP:高效抓取数据的实战指南
在数据驱动的时代,网络爬虫是获取信息的重要工具。本文详解如何用Python结合代理IP抓取数据:从基础概念(爬虫原理与代理作用)到环境搭建(核心库与代理选择),再到实战步骤(单线程、多线程及Scrapy框架应用)。同时探讨反爬策略、数据处理与存储,并强调伦理与法律边界。最后分享性能优化技巧,助您高效抓取公开数据,实现技术与伦理的平衡。
139 4
|
3月前
|
数据采集 数据可视化 大数据
Python入门修炼:开启你在大数据世界的第一个脚本
Python入门修炼:开启你在大数据世界的第一个脚本
108 6
|
3月前
|
数据采集 存储 NoSQL
如何避免Python爬虫重复抓取相同页面?
如何避免Python爬虫重复抓取相同页面?
|
4月前
|
SQL Oracle 关系型数据库
【YashanDB知识库】共享利用Python脚本解决Oracle的SQL脚本@@用法
【YashanDB知识库】共享利用Python脚本解决Oracle的SQL脚本@@用法

推荐镜像

更多