2017-05-09 140 views
0

我是一個網頁抓取的新手。我試圖從here獲得FASTA文件,但不知何故我不能。首先問題開始爲我span標記,我嘗試了幾個建議,但不爲我工作我懷疑可能有一個隱私問題如何使用BeautifulSoup通過網頁抓取seq標籤數據?

該類中的FASTA文件,但是當我運行此代碼時,我可以看到FASTA標題:

url = "https://www.ncbi.nlm.nih.gov/nuccore/193211599?report=fasta" 
res = requests.get(url) 
soup = BeautifulSoup(res.text, "html.parser") 
fasta_data = soup.find_all("div") 
for link in soup.find_all("div", {"class": "seqrprt seqviewer"}): 
    print link.text 

url = "https://www.ncbi.nlm.nih.gov/nuccore/193211599?report=fasta" 
res = requests.get(url) 
soup = BeautifulSoup(res.text, "html.parser") 
fasta_data = soup.find_all("div") 
for link in soup.find_all("div", {"class": "seqrprt seqviewer"}): 
    print link.text 

##When I try to reach directly via span, output is empty. 
div = soup.find("div", {'id':'viewercontent1'}) 
spans = div.find_all('span') 
for span in spans: 
    print span.string 

回答

0

每刮的工作包括兩個階段:

  1. 知道你是想刮的頁面。 (它是如何工作的?內容來自阿賈克斯裝?重定向?POST?GET?I幀?antiscraping東西?......)

  2. 使用您喜歡的框架

不要寫一行模擬網頁之前的代碼工作在第1點。谷歌網絡監察員是你的朋友,使用它!

關於你的網頁,似乎該報告被加載到瀏覽器上從該網址獲取數據:

https://www.ncbi.nlm.nih.gov/sviewer/viewer.fcgi?id=193211599&db=nuccore&report=fasta&extrafeat=0&fmt_mask=0&retmode=html&withmarkup=on&tool=portal&log $ = seqview & maxdownloadsize = 1000000

使用該網址,你會得到您的報告。

相關問題