我目前正在尝试从ncbi蛋白质数据库中提取蛋白质序列。在这一点上,用户可以搜索一种蛋白质,我可以得到数据库输出的第一个结果的链接。但是,当我在美丽的汤中运行这个时,汤与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和使用不同的解析器。我仍然不明白为什么这些不匹配。(抱歉,如果这是一个重复的问题,我对网络垃圾很陌生,目前有脑震荡,所以我正在寻找一些个案反馈)

最佳答案

此代码将使用硒提取所需的蛋白质序列。我修改了你原来的代码,给你想要的结果。

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”产生以下输出:
meepqsdlsielplsqetfsdlwkllppnnvlstlpssdsieelflsenvtgwledsggalqgvaaaaastaedpvtetpapvasapatpwplsssvpsyktfqgdygfrlgflhsgtaksvtctyspslnklfcqlaktcpvqlwvnstpppgtrvramaiykklqymtevvrrcphherssegdslappqhlirvegnlhaeylddkqtfrhsvvvpyeppevgsdcttihynymcnsscmggmnrrpiltiitledpsgnllgrnsfevricacpgrdrrteeknfqkkgepcpelppksakralptntssspppkkktldgeyftlkirgherfkmfqelnealelkdaqaskgsedngahssylkskkgqsasrlkklmikregpdsd

关于python - Python Web抓取时beautifulSoup与chrome inspect不匹配,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50012384/

10-11 14:17