R語言分析技術(shù) 擴(kuò)增子專題 基于phyloseq的微生物群落分析 代謝組專題 當(dāng)科研遇見python 雜談 所需模塊 定義函數(shù) 掃描下方二維碼加入群聊 當(dāng)科研遇見python python爬蟲爬取nature網(wǎng)站 歷史目錄
很高興開展這一專欄的寫作,本專欄作者抱起大塊塊將python之道結(jié)合科學(xué)研究以別樣的方式讓我們逐漸明朗--當(dāng)科研遇見python 兩者會(huì)產(chǎn)生怎樣的火花呢? 
下面來看看我們抱起大塊塊的表演: python爬蟲爬取nature網(wǎng)站我們知道nature是開放性期刊,并且是靜態(tài)的,爬取非常容易,今天我將為演示如何通過關(guān)鍵詞,使用python爬取nature網(wǎng)站。 本函數(shù)運(yùn)行 所需模塊import requests import bs4 from bs4 import BeautifulSoup import traceback import re import time from fake_useragent import UserAgent from pandas import Series,DataFrame import pandas as pd import numpy as np
定義函數(shù)#獲取url def getHTMLText(url):#獲取url try: ua = UserAgent() user_agent = ua.random print(user_agent) headers = {'User-Agent': user_agent, 'Connection': 'close'} r = requests.get(url, timeout = 30,headers=headers) r.raise_for_status() r.encoding = r.apparent_encoding return r.text except: print('網(wǎng)絡(luò)連接錯(cuò)誤')
下載PDF函數(shù) def downPDF(tag): for i in range(len(tag)):#下載pdf downURL='https://www./articles/'+herf[i]+'.pdf' r = requests.get(downURL) f=open('生成文件/'+herf[i]+'.pdf','wb') f.write(r.content) f.close()
獲得文獻(xiàn)ID def getID(html,herf):#獲得文獻(xiàn)ID tag = re.findall(r'href="/articles/s\w*-\w*-\w*-\w*', html) # 獲取id for i in range(len(tag)): herf.append(tag[i].split('s/')[1])
獲取DOI號(hào) def getList(herf,list): for i in herf: r = requests.get('https://www./articles/'+i) print(i) html = r.text soup = BeautifulSoup(html,'html.parser') title = soup.find(attrs={'name':'dc.title'})['content'] #publisher =soup.find(attrs={'name':'dc.publisher'})['content'] description =soup.find(attrs={'name':'dc.description'})['content'] doi = soup.find(attrs={'name':'prism.doi'})['content'] creator =soup.find(attrs={'name':'dc.creator'})['content'] list.append(title) list.append(creator) list.append(doi) list.append(description) time.sleep(5) #list.append(doi)
計(jì)數(shù)函數(shù) def printWenxianList(list): tplt = '{:10}\t{:10}\t{:10}\t{:10}\t{:30}' print(tplt.format("序號(hào)","題目","作者","DOI","摘要")) count = 0 for g in list: count=count+1 print(tplt.format(count,g[0],g[1],g[2],g[3]))'''
主函數(shù) 這里設(shè)置查找10頁,根據(jù)自己需求更改頁數(shù) def main(): key = input('請輸入關(guān)鍵字') #key='iron' depth =10 start_url ='https://www./search?q='+key herf = [] list = [] for i in range(1,depth): try: if i==1: url=start_url else: url=start_url+'&page='+str(i) html = getHTMLText(url) getID(html,herf) except: print('程序錯(cuò)誤') print(herf) getList(herf,list) print(list) data = np.array(list).reshape(int(len(list) / 4), 4) df = DataFrame(data, columns=['title', 'author', 'doi', '摘要']) df.to_csv('生成文件/ceshi.csv', sep='?') main() #print(herf) #print(herf)
按照以上思路,我又寫了:微生物領(lǐng)域的三個(gè)期刊。如果有需要可以連續(xù)我進(jìn)行代碼的索取或者加入群聊:微生信生物  專欄作者介紹: 抱起大快快,熱衷于python,希望自己的生活科研可以擁抱python。嘗試使用爬蟲,科學(xué)數(shù)據(jù)處理,機(jī)器學(xué)習(xí)等處理科研中遇到的小困難??傊敢鈱ython交給科研,兩者究竟會(huì)如何摩擦,接下來的一段日子就拭目以待。 掃描下方二維碼加入群聊歷史目錄R語言分析技術(shù)《擴(kuò)增子16s核心OTU挑選-基于otu_table的UpSet和韋恩圖》 《分類堆疊柱狀圖順序排列及其添加合適條塊標(biāo)簽》 《R語言繪制帶有顯著性字母標(biāo)記的柱狀圖》 《使用R實(shí)現(xiàn)批量方差分析(aov)和多重比較(LSD)》 Rstudio切換掛載R版本及本地安裝一些包 玩轉(zhuǎn)R包
擴(kuò)增子專題《16s分析之Qiime數(shù)據(jù)整理+基礎(chǔ)多樣性分析》 《16s分析之差異展示(熱圖)》 迅速提高序列拼接效率,得到后續(xù)分析友好型輸入,依托qiime https://mp.weixin.qq.com/s/6zuB9JKYvDtlomtAlxSmGw》 16s分析之網(wǎng)絡(luò)分析一(MENA) 16s分析之進(jìn)化樹+差異分析(一) 16s分析之進(jìn)化樹+差異分析(二) Qiime2學(xué)習(xí)筆記之Qiime2網(wǎng)站示例學(xué)習(xí)筆記 PCA原理解讀 PCA實(shí)戰(zhàn) 16s分析之LEfSe分析
基于phyloseq的微生物群落分析開年工作第一天phyloseq介紹 phyloseq入門
代謝組專題非靶向代謝組學(xué)數(shù)據(jù)分析連載(第零篇引子) 非靶向代謝組學(xué)分析連載(第一篇:缺失數(shù)據(jù)處理、歸一化、標(biāo)準(zhǔn)化)
當(dāng)科研遇見python雜談我的生物信息之路
|