根據(jù)特定的SNP list, 在千人基因組數(shù)據(jù)庫中爬取CHB人群的等位基因頻率信息,如https://www.ncbi.nlm.nih.gov/variation/tools/1000genomes/?q=rs12340895。
因為網(wǎng)頁是動態(tài)的數(shù)據(jù),嵌入了JavaScript代碼,因此借助selenium來爬取信息。
Beautiful Soup是Python的一個庫,最主要的功能是從網(wǎng)頁抓取數(shù)據(jù)。Beautiful Soup提供一些簡單的、python式的函數(shù)用來處理導航、搜索、修改分析樹等功能。它是一個工具箱,通過解析文檔為用戶提供需要抓取的數(shù)據(jù),避免了用繁雜的正則表達式。
1、安裝chromedriver: brew install chromedriver
注意:使用該命令安裝的時候可能安裝的chromedriver不是最新版,有可能導致與chrome瀏覽器版本不兼容而報如下錯誤:
unknown error: Runtime.executionContextCreated has invalid ‘context’: {“auxData”:{“frameId”:”11740.1”,”isDefault”:true},”id”:1,”name”:”“,”origin”:”://”}
(Session info: chrome=54.0.2840.71)
(Driver info: chromedriver=2.9.248307,platform=Mac OS X 10.9.4 x86_64)
這個其實是老版本的chromedriver 無法正常啟動chrome。解決辦法就是下載最新的chromedriver.
如果用brew upgrade命名更新不了chromedriver,要去https://sites.google.com/a/chromium.org/chromedriver/downloads 下載剛發(fā)布的Latest Release: ChromeDriver 2.25
2、下載完畢chromedriver,Move the file to /usr/bin directory sudo mv chromedriver /usr/bin
# -*- coding:utf-8 -*-from bs4 import BeautifulSoupimport timefrom selenium import webdriverfrom selenium.common.exceptions import NoSuchElementExceptiondef get_allele_feq(browser, snp): browser.get( 'https://www.ncbi.nlm.nih.gov/variation/tools/1000genomes/?q=%s' %snp) #Load page # browser.implicitly_wait(60) #智能等待xx秒 time.sleep(30) #加載時間較長,等待加載完畢 # browser.find_element_by_css_selector("div[title=\"Han Chinese in Bejing, China\"]") #use selenium function to find elements # 把selenium的webdriver調用page_source函數(shù)在傳入BeautifulSoup中,就可以用BeautifulSoup解析網(wǎng)頁了 bs = BeautifulSoup(browser.page_source, "lxml") # bs.find_all("div", title="Han Chinese in Bejing, China") try: race = bs.find(string="CHB") race_data = race.find_parent("div").find_parent( "div").find_next_sibling("div") # print race_data race_feq = race_data.find("span", class_="gt-selected").find_all("li") # class_ 防止Python中類關鍵字重復,產生語法錯誤 base1_feq = race_feq[0].text #獲取標簽的內容 base2_feq = race_feq[1].text return snp, base1_feq, base2_feq # T=0.1408 C=0.8592 except NoSuchElementException: return "%s:can't find element" %snp def main(): browser = webdriver.Chrome() # Get local session of chrome fh = open("./4diseases_snps_1kCHB_allele_feq.list2", 'w') snps = open("./4diseases_snps.list.uniq2",'r') for line in snps: snp = line.strip() response = get_allele_feq(browser, snp) time.sleep(1) fh.write("\t".join(response)) #unicode 編碼的對象寫到文件中后相當于print效果 fh.write("\n") print "\t".join(response) time.sleep(1) # sleep a few seconds fh.close() browser.quit() # 退出并關閉窗口的每一個相關的驅動程序if __name__ == '__main__': main()
1]: http://beautifulsoup.readthedocs.io/zh_CN/latest/ #Beautiful Soup 4.4.0 文檔
2]: http://blog.csdn.net/buptlrw/article/details/48828201
3]: http://www.cnblogs.com/duyang/p/5144987.html
4]: http://blog.csdn.net/cjsafty/article/details/9206323
5]: http://stackoverflow.com/questions/8255929/running-webdriver-chrome-with-selenium
6]: https://pypi.python.org/pypi/selenium#downloads
7]: http://blog.csdn.net/leejeff/article/details/52935706
聯(lián)系客服