Python 3.6 BeautifulSoup 循环后打印空白区域

2024-04-29 07:53:09 发布

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

我使用的是beautifulsoup页面上的“三姐妹”HTML(贴在这段下面)。我对beautiful soup还不太熟悉,我一辈子都不能让这个循环输出我可以在for/in循环中使用的信息。我可以让名字单独打印出来,但我不能让它们充当变量。你知道吗

<pre>
<html>
    <head>
        <title>
            The Dormouse's story
        </title>
    </head>
    <body>
        <b></b>
        <p class="title">
            <b>
                The Dormouse's story
            </b>
        </p>
        <p class="story">
            Once upon a time there were three little sisters; and their names were
            <a class="sister" href="http://example.com/elsie" id="link1">
                Elsie
            </a>
            ,
            <a class="sister" href="http://example.com/lacie" id="link2">
                Lacie
            </a>
                and
            <a class="sister" href="http://example.com/tillie" id="link2">
                Tillie
            </a>
                ; and they lived at the bottom of a well.
        </p>
        <p class="story">
            <b>
                The End
            </b>
        </p>
    </body>
</html>
</pre>

我认为这是因为它们不仅仅是“Elsie”(或者我选择的任何名字),或者从技术上来说它们不是一个字符串。这是代码,我在pycharm中只得到一个空格,然后进程结束,退出代码为0。所以没有错误。。。你知道吗

我怎么知道发生了什么事?或者,在循环之后,如何将输出转换为一个名称字符串?你知道吗

代码:

from bs4 import BeautifulSoup

def main_file_open():
    file = open('three-sisters.html')
    data = file.read()
    file.close()
    return data

soup = BeautifulSoup(main_file_open(),'lxml')

attr = {'class':'sister'}
father = soup.find_all('a',attrs=attr)

for child in father:
    if child == "Elsie":
        print(child)

在pycharm的“run”窗口中出现以下内容。你知道吗

<blank space>
Process finished with exit code 0

然后它输出一个空白,没有错误,什么都没有。所以它显然没有感觉到“埃尔西”在弦里面。你知道吗

我想看到的是

Elsie

如果我把它改成:

 `if child != 'Elsie':` 

然后输出所有三个名称:

<a class="sister" href="http://example.com/elsie" id="link1">
                Elsie
            </a>
<a class="sister" href="http://example.com/lacie" id="link2">
                Lacie
            </a>
<a class="sister" href="http://example.com/tillie" id="link2">
                Tillie
            </a>

所以我一直在想到底发生了什么,因为如果我遇到一个只想打印某些“children”的情况,那么我该如何将它们从soup标记转换成字符串,以便在for x in语句中使用它们?你知道吗

就像我说的,我知道我可以通过打印得到一个“字符串”(子.string)但它不会把它变成一个变量。你知道吗

attr = {'class':'sister'}
father = soup.find_all('a',attrs=attr)

for child in father:
    print(child.string)

产生:

            Elsie


            Lacie


            Tillie

如果我这么做了:

attr = {'class':'sister'}
father = soup.find_all('a',attrs=attr)

for child in father:
    print(child.string)

for child in father:
    if child == 'Elsie':
        print(child)

它仍然只打印:

            Elsie


            Lacie


            Tillie

Tags: incomidchildhttpforexamplesister
1条回答
网友
1楼 · 发布于 2024-04-29 07:53:09

如果您检查father中的内容,您将看到:

[<a class="sister" href="http://example.com/elsie" id="link1">
                Elsie
            </a>, <a class="sister" href="http://example.com/lacie" id="link2">
                Lacie
            </a>, <a class="sister" href="http://example.com/tillie" id="link2">
                Tillie
            </a>]

find_all()方法返回与给定参数匹配的标记的列表。所以,当您选中if child == "Elsie":时,您将一个字符串与整个标记进行比较。比如:

if '<a class="sister" href="http://example.com/elsie" id="link1">  Elsie </a>' == "Elsie":

因此,即使字符串Elsie出现在标记中,它也会返回False。你知道吗

要检查标记是否具有所需的字符串,可以使用.text方法,该方法将返回“Elsie”(带有空格和换行符)。所以你需要strip()它。你知道吗

最后,在进行这些更改时,您可以使用:

for child in father:
    if child.text.strip() == 'Elsie':
        print(child)

或者,只需按以下方式打印文本:

for child in father:
    if child.text.strip() == 'Elsie':
        print(child.text.strip())

相关问题 更多 >