您的位置:首页 > 其它

pycurl 使用 跳转

2016-01-20 10:35 127 查看

pycurl 使用 跳转

根据doi获取文献的最终url,而不获得原文(如想获得全文,可以修改 FOLLOWLOCATION为1)

doi='10.1007/s12559-015-9328-x'
storage = io.BytesIO() # python3, python2与此处不同,存储字符串
c = pycurl.Curl() # 初始化对象
c.setopt(c.URL, 'http://dx.doi.org/'+doi) # 设置url
c.setopt(pycurl.FOLLOWLOCATION, 0) #是否重定向到url,0表示关闭重定向,1表示开启重定向
# c.setopt(c.HTTPHEADER,["Accept: application/vnd.crossref.unixsd+xml"]) #设置header
c.setopt(pycurl.WRITEFUNCTION, storage.write)# 将返回的字符串写入storage
# c.setopt(pycurl.HEADERFUNCTION, storage.write)# 将返回的header写入storage
c.perform() # 执行

# out = c.getinfo(pycurl.EFFECTIVE_URL)# 获取最新的url,如果重定向,则返回跳转后的url
content = storage.getvalue().decode('utf-8')#返回byte,需要进行decode,如果 c.setopt(pycurl.FOLLOWLOCATION, 1)设置为1,则返回跳转后的全文

#返回数据进行正则表达式匹配,获取重定向url
out = re.compile('(?<=href=").*(?=">)').findall(content)[0]
cu_w.execute('insert into doi_link values(%s, %s, %s)', (pmid,doi, out))
con.commit()
c.close()
storage.close()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: