如何解析执行javascrip的html网页

2024-06-19 19:13:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试编写一个程序,在this webpage上为IUPACcondensed创建一个片段。你知道吗

这里G03307GFID。我需要这个:

HexNAc(b1-?)[Fuc(a1-?)]GlcNAc(b1-2)Man(a1-3)[HexNAc(b1-?)[Fuc(a1-?)]GlcNAc(b1-2)Man(a1-6)]Man(b1-4)GlcNAc(b1-4)[Fuc(a1-6)]GlcNAc

我试着用硒来做这个。你知道吗

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options.add_argument('--headless')
driver = webdriver.Chrome('', options = chrome_options)

# takes accession number and returns IUPAC
def getIUPAC(acc_no):

    url = 'https://glytoucan.org/Structures/Glycans/' + acc_no

    driver.get(url)
    IUPAC = driver.find_element_by_xpath('//*[@id="descriptors"]/togostanza-iupaccondensed//main/div/pre/code/text()')
    driver.close()

    return IUPAC

IUPAC = getIUPAC('G37498VS')

print(IUPAC)

It says the the element does not exist.


Tags: fromimporta1driverseleniumchromeb1options
2条回答

更好地使用毒液所显示的请求。只是想补充一下,你得到的是element does not exist,因为驱动程序在你打印之前已经关闭了。你知道吗

import re
import requests

def getIUPAC(acc_no):
    ret = requests.get('https://glytoucan.org/Structures/Glycans/{}'.format(acc_no))
    z = re.search('<meta name="description".*?The IUPAC representation is (.+)\.\s+The', ret.content, re.DOTALL | re.MULTILINE)
    return z if z else 'Unknown'


print('IUPAC is {}'.format(getIUPAC('G03307GF')))

我们的结果是。。。你知道吗

IUPAC is HexNAc(b1-?)[Fuc(a1-?)]GlcNAc(b1-2)Man(a1-3)[HexNAc(b1-?)[Fuc(a1-?)]GlcNAc(b1-2)Man(a1-6)]Man(b1-4)GlcNAc(b1-4)[Fuc(a1-6)]GlcNAc

相关问题 更多 >