2017-12-23 11 views
1

私はclinicalTrials.govからデータをスクレイプする小さなPython関数に取り組んでいます。各研究記録から、研究が対象とする条件を掻き集めたいと思う。例えば、私は次のことをしたいthis研究記録のために:clinicalTrials.govのデータをスクラップ

conditions = ['Rhinoconjunctivitis', 'Rhinitis', 'Conjunctivitis'. 'Allergy'] 

しかし、各研究レコードに、条件の異なる番号があります。

b'Condition or disease' 
b'Intervention/treatment' 
b'Phase' 
b'Rhinoconjunctivitis' 
b'Rhinitis' 
b'Conjunctivitis' 
b'Allergy' 
b'Drug: Placebo' 
b'Biological: SCH 697243' 
b'Drug: Loratadine Syrup 1 mg/mL Rescue Treatment' 
b'Drug: Loratadine 10 mg Rescue Treatment' 
b'Drug: Olopatadine 0.1% Rescue Treatment' 
b'Drug: Mometasone furoate 50 mcg Rescue Treatment' 
b'Drug: Albuterol 108 mcg Rescue Treatment' 
b'Drug: Fluticasone 44 mcg Rescue Treatment' 
b'Drug: Prednisone 5 mg Rescue Treatment' 
b'Phase 3' 

は、どのように私は今だけ介入/治療情報なし条件を得ることができますので、同じよう

page = requests.get('https://clinicaltrials.gov/ct2/show/study/NCT00550550') 
soup = BeautifulSoup(page.text, 'html.parser') 
studyDesign = soup.find_all(headers='studyInfoColData') 
condition = soup.find(attrs={'class':'data_table'}).find_all('span') 
for each in condition: 
    print(each.text.encode('utf-8').strip()) 

:私は、データを取得し、次のスクリプトを書かれていますか?

答えて

1

あなただけのtdにクラスdata_table &エキスspan要素を持つ最初のtableを使用することができます。

import requests 
from bs4 import BeautifulSoup 

page = requests.get('https://clinicaltrials.gov/ct2/show/study/NCT00550550') 
soup = BeautifulSoup(page.text, 'html.parser') 
studyDesign = soup.find("table", {"class" : "data_table"}).find('td') 
conditions = [ t.text.strip() for t in studyDesign.find_all('span') ] 
print(conditions) 

与える:

[u'Rhinoconjunctivitis', u'Rhinitis', u'Conjunctivitis', u'Allergy'] 
1

はたぶん、このコードは役立ちます。

import requests 
from bs4 import BeautifulSoup 

#url = "https://clinicaltrials.gov/ct2/show/NCT02656888" 
url = "https://clinicaltrials.gov/ct2/show/study/NCT00550550" 

page = requests.get(url) 
soup = BeautifulSoup(page.content, 'html.parser') 
table = soup.find_all("table", class_="data_table") 

tds = [tr.find_all("td") for tr in table] 
conditions = [condition for condition in (tds[0][0].get_text().split("\n")) if condition != ""] 

print(conditions) 
関連する問題