我想從此網頁獲取uniprot ID:ENSEMBL。但是我在使用xpath時遇到了問題。現在我得到一個空的列表,我不明白爲什麼。使用Xpath從網頁獲取字符串
我的想法是編寫一個採用ENSEMBL ID並返回uniprot ID的小函數。
import requests
from lxml import html
ens_code = 'ENST00000378404'
webpage = 'http://www.ensembl.org/id/'+ens_code
response = requests.get(webpage)
tree = html.fromstring(response.content)
path = '//*[@id="ensembl_panel_1"]/div[2]/div[3]/div[3]/div[2]/p/a'
uniprot_id = tree.xpath(path)
print uniprot_id
任何幫助,將不勝感激:)
它只能打印現有列表,但仍返回Nonetype列表。
def getUniprot(ensembl_code):
ensembl_code = ensembl_code[:-1]
webpage = 'http://www.ensembl.org/id/'+ensembl_code
response = requests.get(webpage)
tree = html.fromstring(response.content)
path = '//div[@class="lhs" and text()="Uniprot"]/following-sibling::div/p/a/text()'
uniprot_id = tree.xpath(path)
if uniprot_id:
print uniprot_id
return uniprot_id
它返回None,因爲當你沒有匹配時,你的函數返回的結果是 –
但是if語句如何得到它?我能做些什麼來避免它? – Suapu
你應該檢查返回值,只是'返回tree.xpath(path)'而忘記if,然後檢查函數外部'ret = getUniprot(「whatever」)'然後'如果ret'使用它 –