你好,我有这样的html,当我用Beautiful Soup解析它时,我无法选择类文本。认为问题出在,因为嵌套标记未被识别为它的子代。 如何选择跨度标签文本?
谢谢
<div data-component="new_enquiry_form_app" data-props="{"isTelRequired":false,"placement":"top",}">
<section class="enquiry-form-box__wrapper">
<div class="enquiry-form-box enquiry-form-box--inverted">
<form class="enquiry-form-box__form" tabindex="-1">
<fieldset class="enquiry-form-box__wrapper">
<div class="enquiry-form-box__fields">
<div class="k-ns">
<span class="text-gray block mt-3 font-bold text-sm">Property reference: 412</span>
</div>
</div>
</fieldset>
</form>
</div>
</section>
答案 0 :(得分:1)
尝试一下:
from bs4 import BeautifulSoup
html = '''<div data-component="new_enquiry_form_app" data-props="{"isTelRequired":false,"placement":"top",}">
<section class="enquiry-form-box__wrapper">
<div class="enquiry-form-box enquiry-form-box--inverted">
<form class="enquiry-form-box__form" tabindex="-1">
<fieldset class="enquiry-form-box__wrapper">
<div class="enquiry-form-box__fields">
<div class="k-ns">
<span class="text-gray block mt-3 font-bold text-sm">Property reference: 412</span>
</div>
</div>
</fieldset>
</form>
</div>
</section>'''
soup = BeautifulSoup(html, 'html.parser')
span = soup.select_one('span.text-gray.block.mt-3.font-bold.text-sm')
print(span.get_text())
打印:
Property reference: 412
然后这是一种方法:
from selenium import webdriver
driver = webdriver.Firefox(executable_path='c:program/geckodriver')
driver.get('https://www.kyero.com/en/property/7689206-villa-for-sale-sant-joan-de-labritja')
span = driver.find_element_by_css_selector('span.text-gray.block.mt-3.font-bold.text-sm')
print(span.text)
driver.close()
打印:
Property reference: 412
请注意,您需要selenium和geckodriver,并且在此代码中,将geckodriver设置为从c:/program/geckodriver.exe
导入
@Andrej Kesely在回答其他问题时速度更快,所以我给出了硒的答案。
答案 1 :(得分:1)
要打印参考标签,可以使用以下脚本(数据存储在HTML文档中的javascript变量中):
import re
import json
import requests
url = 'https://www.kyero.com/en/property/7689206-villa-for-sale-sant-joan-de-labritja'
headers = {'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:78.0) Gecko/20100101 Firefox/78.0'}
html_text = requests.get(url, headers=headers).text
data = json.loads( re.search(r'window\.initialState = (.*);', html_text).group(1) )
# uncomment this to print all data:
# print(json.dumps(data, indent=4))
print(data['property']['referenceLabel'])
打印:
Property reference: 412