使用 BeautifulSoup 提取重复标签中的特定文本

直接和狭隘的问题是subprocess.call()返回退出状态(0如果grep没有失败,或者失败1了),而不是输出。这可以通过使用check_output()来解决:


version = subprocess.check_output(

    "ansible --version | awk '/ansible [0-9].[0-9].[0-9]/ { print $2; exit }'", shell=True

).strip().decode('utf-8')

如果您想避免shell=True(值得称赞,但在您当前的用例中实际上并不是直接的安全问题),这可能如下所示:


import re


av = subprocess.check_output(['ansible', '--version'])

match = re.match('^ansible (\d+[.]\d+[.]\d+)$', av.split(b'\n')[0].decode('utf-8'))

if match is None:

  raise Exception("Unable to get version number from ansible")

version = match.group(1)

如何使用 BeautifulSoup 将描述文本与这些标签中的文本隔离开来?到目前为止,我在 StackOverFlow 上发现的所有内容都表明它可能是可行的;但是我还没有找到专门尝试这样做的东西。

同样,从源代码中,我只想提取描述“施洗约翰为耶稣施洗……”。我怎么能去做这件事?

谢谢!再次为我缺乏扎实的知识而感到抱歉。


吃鸡游戏
浏览 282回答 3
3回答

潇湘沐

在这个例子中,我们可以使用 CSS 选择器。假设你使用的是 BeautifulSoup 4.7+,CSS 选择器支持是由Soupsieve库提供的。我们将首先使用:has()CSS 级别 4 选择器来查找<p>具有直接子<b>标签的标签,然后使用汤筛的非标准:contains选择器来确保<b>标签包含Description:. 然后我们简单地打印所有符合此条件的元素的内容,去除前导和尾随空格并去除Description:. 请记住,有多种方法可以做到这一点,这就是我选择来说明方法:import bs4markup = """</div><div class="col-sm-6"><P>&nbsp; &nbsp; <b>Book Title:</b>&nbsp; &nbsp; <A HREF="book_detail.cfm?ID=2449">The Holy Bible containing the Old and New Testaments, according to the authorised version. With illustrations by Gustave Doré</a></p>&nbsp; &nbsp; <P>&nbsp; &nbsp; &nbsp; &nbsp; <b>Author:</b> Doré, Gustave, 1832-1883&nbsp; &nbsp; </p>&nbsp; &nbsp; <P>&nbsp; &nbsp; &nbsp; &nbsp; <b>Image Title:</b> Baptism of Jesus&nbsp; &nbsp; </p>&nbsp; &nbsp; <P>&nbsp; &nbsp; &nbsp; &nbsp; <b>Scripture Reference:</b><ul><li>John 1 (<a href='search.cfm?biblicalbook=John&biblicalbookchapter=1'>further images</a> / <a rel='shadowbox;height=500;width=600' href='http://www.commonenglishbible.com/explore/passage-lookup/?query=John+1'>scripture text</a>)</li></ul>&nbsp; &nbsp; </p>&nbsp; &nbsp; &nbsp; &nbsp; <P>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <b>Description:</b> John the Baptist baptizes Jesus in the Jordan River; the Holy Spirit appears overhead in the form of a dove. The artist, Gustave Doré (1832-1883), has placed his signature at the lower left of the woodcut, and the engraver’s signature, A. Ligny, is located at the lower right.&nbsp; &nbsp; &nbsp; &nbsp; </P>&nbsp; &nbsp; <P>&nbsp; &nbsp; &nbsp; &nbsp; <A HREF="book_list.cfm?ID=2449">Click here&nbsp; &nbsp; &nbsp; &nbsp; </a> for additional images available from this book.&nbsp; &nbsp; </P>&nbsp; &nbsp; <p>For information on licensing this image, please send an email, including a link to the image, to&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; <a href="mailto:dia@emory.edu?subject=Licensing%20Image%20From%20DIA - 17250">dia@emory.edu</a>&nbsp; &nbsp; </p></div>"""soup = bs4.BeautifulSoup(markup, "html.parser")for el in soup.select('p:has(> b:contains("Description:"))'):&nbsp; &nbsp; print(el.get_text().strip('').replace('Description: ', ''))输出:John the Baptist baptizes Jesus in the Jordan River; the Holy Spirit appears overhead in the form of a dove. The artist, Gustave Doré (1832-1883), has placed his signature at the lower left of the woodcut, and the engraver’s signature, A. Ligny, is located at the lower right.&nbsp;

幕布斯6054654

我可以使用以下代码实现几乎像您想要的东西:import urllib.requestimport urllib.parsefrom bs4 import BeautifulSoupurl = "http://pitts.emory.edu/dia/image_details.cfm?ID=17250"f = urllib.request.urlopen(url)soup = BeautifulSoup(f, 'html.parser')parent = soup.find("b", text="Description:").parentparent.find("b", text="Description:").decompose()print(parent.text)我添加了 BeautifulSoup 并删除了描述。

鸿蒙传说

我使用 < p > 标签作为索引,然后选择了 [4] 索引。我只是一个新手,但它奏效了。from urllib.request import urlopenfrom bs4 import BeautifulSouphtml = urlopen("http://pitts.emory.edu/dia/image_details.cfm?ID=17250")soup = BeautifulSoup(html, 'html.parser')page = soup.find_all('p')[4].getText()print(page)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python