python - beautifulSoup 在 Python 网络抓取时不匹配 chrome inspect

标签 python html web-scraping beautifulsoup python-requests

我目前正在尝试从 ncbi 蛋白质数据库中抓取蛋白质序列。此时,用户可以搜索蛋白质,我可以获得数据库吐出的第一个结果的链接。但是,当我通过漂亮的汤运行它时,汤与 chrome 检查元素不匹配,也根本没有序列。

这是我当前的代码:

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 和使用不同的解析器。我仍然对为什么这些不匹配感到困惑。 (抱歉,如果这是一个重复的问题,我对网络抓取很陌生,目前有脑震荡,所以我正在寻找一些个案反馈)

最佳答案

此代码将使用 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 - beautifulSoup 在 Python 网络抓取时不匹配 chrome inspect,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50012384/

相关文章:

python - 带条件保存文件名

python - 使用 ffmpeg 动态更改视频裁剪宽度、高度、x 和 y

html - 将 Logo 图像与导航栏对齐

javascript - 连接 Html 表单中的用户输入并为其分配 ID,以便在 JavaScript 中提交

python - f-strings 给出 SyntaxError?

python - 在 'for' 循环中处理 dtype

jquery - 如何使用 CSS 从图像中去除颜色?

python - 如何将非英文字符串存储到excel文件中,python3?

html - 使用下拉菜单中的选项从结果页面下载 CSV 文件

python - 刚刚学会了抓取数据并将其存储在我的数据库中,但它只抓取了一个对象