无法计算出此HTML的BeautifulShudfind()命令怎么办?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (42)

我正试图从一个用python和Beautiful soup的页面中获取一些信息,我似乎无法为我所需要的写正确的路径,html是:

<div class="operator active" data-operator_name="Etisalat" data- 
operator_id="5"><div class="operator_name_etisalat"></div></div>

我试着把接线员operator的名字叫“Etisalat”,我得到了这么多:

def list_contries():

select = Select(driver.find_element_by_id('international_country'))
select.select_by_visible_text('France')
request = requests.get("https://mobilerecharge.com/buy/mobile_recharge?country=Afghanistan&operator=Etisalat")
content = request.content
soup = BeautifulSoup(content, "html.parser")
# print(soup.prettify())
prov=soup.find("div", {"class": "operator active"})['data-operator_name']
# prov = soup.find("div", {"class": "operator deselected"})
print(prov)
operator = (prov.text.strip())

但是这只是返回一个NoneType.所以有些地方不对劲,谁能告诉我做错了什么?

提问于
用户回答回答于

你可以使用CSS选择器,CSS选择器[data-operator_name]将选择任何带有属性的标记:

data = """<div class="operator active" data-operator_name="Etisalat" data-
operator_id="5"><div class="operator_name_etisalat"></div></div>"""

from bs4 import BeautifulSoup

soup = BeautifulSoup(data, 'lxml')

print(soup.select_one('[data-operator_name]')['data-operator_name'])

这将输出:

Etisalat

使用属性“数据运算符”选择多个标记_姓名“,使用.select()方法:

data = """<div class="operator active" data-operator_name="Etisalat" data-
operator_id="5"><div class="operator_name_etisalat"></div></div>"""

from bs4 import BeautifulSoup

soup = BeautifulSoup(data, 'lxml')
for tag in soup.select('[data-operator_name]'):
    print(tag['data-operator_name'])

扫码关注云+社区

领取腾讯云代金券