gpt4 book ai didi

python - findChildren() 方法存储两个相同的 child 而不是一个

转载 作者:行者123 更新时间:2023-11-27 23:12:22 24 4
gpt4 key购买 nike

在我使用 urllib2 打开并使用 BeautifulSoup 抓取的网页中,我试图在网页中存储特定文本。

在您看到代码之前,这里是网页中 HTML 屏幕截图的链接,以便您了解我使用 BeautifulSoup 中的 find 函数的方式:

HTML from webpage

最后,这是我使用的代码:

from BeautifulSoup import BeautifulSoup
import urllib2

url = 'http://www.sciencekids.co.nz/sciencefacts/animals/bird.html'
page = urllib2.urlopen(url)
soup = BeautifulSoup(page.read())

ul = soup.find('ul', {'class': 'style33'})
children = ul.findChildren()
for child in children:
print child.text

这是我的问题所在的输出:

Birds have feathers, wings, lay eggs and are warm blooded.
Birds have feathers, wings, lay eggs and are warm blooded.
There are around 10000 different species of birds worldwide.
There are around 10000 different species of birds worldwide.
The Ostrich is the largest bird in the world. It also lays the largest eggs and has the fastest maximum running speed (97 kph).
The Ostrich is the largest bird in the world. It also lays the largest eggs and has the fastest maximum running speed (97 kph).
Scientists believe that birds evolved from theropod dinosaurs.
Scientists believe that birds evolved from theropod dinosaurs.
Birds have hollow bones which help them fly.
Birds have hollow bones which help them fly.
Some bird species are intelligent enough to create and use tools.
Some bird species are intelligent enough to create and use tools.
The chicken is the most common species of bird found in the world.
The chicken is the most common species of bird found in the world.
Kiwis are endangered, flightless birds that live in New Zealand. They lay the largest eggs relative to their body size of any bird in the world.
Kiwis are endangered, flightless birds that live in New Zealand. They lay the largest eggs relative to their body size of any bird in the world.
Hummingbirds can fly backwards.
Hummingbirds can fly backwards.
The Bee Hummingbird is the smallest living bird in the world, with a length of just 5 cm (2 in).
The Bee Hummingbird is the smallest living bird in the world, with a length of just 5 cm (2 in).
Around 20% of bird species migrate long distances every year.
Around 20% of bird species migrate long distances every year.
Homing pigeons are bred to find their way home from long distances away and have been used for thousands of years to carry messages.
Homing pigeons are bred to find their way home from long distances away and have been used for thousands of years to carry messages.

是不是我在代码中使用不当和/或做错了什么,导致本应只有一个的 child 却出现了两个?创建一些额外的代码很容易,这样我就不会存储相同信息的重复项,但我宁愿以正确的方式执行此操作,这样我只得到我要查找的每个字符串之一。

最佳答案

children = ul.findChildren()正在同时选择 <li><p><ul> 内.遍历 children导致您打印 text这两个元素的属性。要解决此问题,只需更改 children = ul.findChildren()children = ul.findChildren("p") .

关于python - findChildren() 方法存储两个相同的 child 而不是一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45308955/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com