【问题标题】:BeautifulSoup doesn't find all spans or childrenBeautifulSoup 没有找到所有跨度或子项
【发布时间】:2018-05-22 05:00:37
【问题描述】:

我正在尝试访问此网页上的序列:
https://www.ncbi.nlm.nih.gov/protein/EGW15053.1?report=fasta

序列存储在 div class="seq gbff" 下。每一行都存放在

<span class='ff_line' id='gi_344258949_1"> *line 1 of sequence* </span>

当我尝试搜索包含该序列的 span 时,beautiful soup 返回 None。当我尝试查看spans 上方的div 的子项或内容时,同样的问题。

代码如下:

import requests
import re
from bs4 import BeautifulSoup
# Create a variable with the url
url = 'https://www.ncbi.nlm.nih.gov/protein/EGW15053.1?report=fasta'

# Use requests to get the contents
r = requests.get(url)

# Get the text of the contents
html_content = r.text

# Convert the html content into a beautiful soup object
soup = BeautifulSoup(html_content, 'html.parser')


div = soup.find_all('div', attrs={'class', 'seq gbff'})
for each in div.children:
    print(each)
soup.find_all('span', aatrs={'class', 'ff_line'})

这两种方法都不起作用,我将非常感谢任何帮助:D

【问题讨论】:

    标签: python beautifulsoup


    【解决方案1】:

    此页面使用 JavaScript 加载数据

    在 Chrome/Firefox 中使用DevTools,我找到了这个网址,里面全是&lt;span&gt;

    https://www.ncbi.nlm.nih.gov/sviewer/viewer.fcgi?id=344258949&db=protein&report=fasta&extrafeat=0&fmt_mask=0&retmode=html&withmarkup=on&tool=portal&log$=seqview&maxdownloadsize=1000000

    现在是困难的部分。您必须在 HTML 中找到此 url,因为不同的页面将在 url 中使用不同的参数。或者您必须比较几个 url 并找到架构,以便您可以手动生成此 url。


    编辑:如果您在 url 中将 retmode=html 更改为 retmode=xml,那么您将得到 XML。如果你使用retmode=text,那么你会得到没有HTML标签的文本。 retmode=json 不起作用。

    【讨论】:

    • 查看新文本“困难部分”
    • 在回答中查看retmode=text
    • 这对我来说实际上很容易。 div = soup.find_all('div', attrs={'class', 'seq gbff'}) 包含我要访问的每个页面的唯一值,只需替换每个 url 中的 id。
    • 我检查了url只需要三个参数就可以获取数据id=344258949&amp;report=fasta&amp;retmode=text
    猜你喜欢
    • 2016-05-27
    • 2018-03-28
    • 2015-02-05
    • 1970-01-01
    • 2021-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-21
    相关资源
    最近更新 更多