美化组与Python webscraping时chrome inspect不匹配

2024-04-26 14:20:33 发布

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

我正在尝试从ncbi蛋白质数据库中提取蛋白质序列。在这一点上,用户可以搜索一个蛋白质,我可以得到数据库显示的第一个结果的链接。但是,当我在beautiful soup中运行这个时,这个汤与chrome inspect元素不匹配,也根本没有序列。在

以下是我当前的代码:

import string
import requests
from bs4 import BeautifulSoup

def getSequence():
    searchProt = input("Enter a Protein Name!:")
    if searchProt != '':
        searchString = "https://www.ncbi.nlm.nih.gov/protein/?term=" + searchProt
        page = requests.get(searchString)
        soup = BeautifulSoup(page.text, 'html.parser')
        soup = str(soup)
        accIndex = soup.find("a")
        accessionStart = soup.find('<dd>',accIndex)
        accessionEnd = soup.find('</dd>', accessionStart + 4)
        accession = soup[accessionStart + 4: accessionEnd]
        newSearchString = "https://www.ncbi.nlm.nih.gov/protein/" + accession
        try:
            newPage = requests.get(newSearchString)
            #This is where it fails
            newSoup = BeautifulSoup(newPage.text, 'html.parser')
            aaList = []
            spaceCount = newSoup.count("ff_line")
            print(spaceCount)
            for i in range(spaceCount):
                startIndex = newSoup.find("ff_line")
                startIndex = newSoup.find(">", startIndex) + 2
                nextAA = newSoup[startIndex]
                while nextAA in string.ascii_lowercase:
                    aaList.append(nextAA)
                    startIndex += 1
                    nextAA = newSoup[startIndex]
            return aaList        
         except:
            print("Please Enter a Valid Protein")

我一直在尝试用搜索“p53”运行它,并找到了链接:here

我查看了这个网站上的一长串网络垃圾条目,并尝试了很多事情,包括安装selenium和使用不同的解析器。我仍然不明白为什么这些不匹配。(很抱歉,如果这是一个重复的问题,我是一个非常新的网站拉屎,目前有一个脑震荡,所以我正在寻找一些个别案件的反馈)


Tags: import数据库ncbi蛋白质findrequestssoupbeautifulsoup
1条回答
网友
1楼 · 发布于 2024-04-26 14:20:33

这段代码将使用Selenium提取您想要的蛋白质序列。我已经修改了你的原始代码以获得你想要的结果。在

from bs4 import BeautifulSoup
from selenium import webdriver
import requests

driver = webdriver.Firefox()

def getSequence():
    searchProt = input("Enter a Protein Name!:")
    if searchProt != '':
        searchString = "https://www.ncbi.nlm.nih.gov/protein/?term=" + searchProt
        page = requests.get(searchString)
        soup = BeautifulSoup(page.text, 'html.parser')
        soup = str(soup)
        accIndex = soup.find("a")
        accessionStart = soup.find('<dd>',accIndex)
        accessionEnd = soup.find('</dd>', accessionStart + 4)
        accession = soup[accessionStart + 4: accessionEnd]
        newSearchString = "https://www.ncbi.nlm.nih.gov/protein/" + accession
        try:
            driver.get(newSearchString)
            html = driver.page_source
            newSoup = BeautifulSoup(html, "lxml")
            ff_tags = newSoup.find_all(class_="ff_line")
            aaList = []
            for tag in ff_tags:
                aaList.append(tag.text.strip().replace(" ",""))
            protSeq = "".join(aaList)
            return protSeq
        except:
            print("Please Enter a Valid Protein")

sequence = getSequence()
print(sequence)

为输入“p53”生成以下输出:

^{pr2}$

相关问题 更多 >