如何使BeautifulSoup找到多个标签?

时间:2020-06-10 23:00:49

标签: python python-3.x

我要测试一个程序,以将Wikipedia的内容打印到提示中。我已经得到了一些输出,但是有点混乱。因此,我认为仅获取标签<p><b>的内容,而这两个标签是Wikipedia用来显示内容的。这是我的代码:

import urllib.request
from bs4 import BeautifulSoup


URL = input("Enter the url (only wikipedia supported, default url https://?.wikipedia.org/wiki) :  ")



page = urllib.request.urlopen(URL)
html_doc = page.read()
soup = BeautifulSoup(html_doc, 'html.parser')





for x in soup.find_all('p').find_all('b'):
    print(x.string)

该询问标记是因为Wikipedia在那里显示了语言,因此取决于语言。如您所见,我添加了一个.find_all,因为我不知道如何添加它。对不起,我的英语不好和我的代码错误,因为我与此请求字段没有太大关系。谢谢

1 个答案:

答案 0 :(得分:0)

def interpolate(x1: float, x2: float, y1: float, y2: float, x: float): """Perform linear interpolation for x between (x1,y1) and (x2,y2) """ return ((y2 - y1) * x + x2 * y1 - x1 * y2) / (x2 - x1) 返回一个BeautifulSoup.find_all,它实际上是元素列表。您需要自己遍历该列表以进行第二次操作。

ResultSet