繁体   English   中英

Python 美丽的汤:从元素中获取文本

[英]Python Beautiful Soup: get text from element

我正在循环遍历<td>类型的元素,但正在努力提取<td>文本。

HTML:

<td class="cell">
 Brand Name 1
 <br/>
 (
 <a class="tip" title="This title">
  Authorised Resellers
 </a>
 )
</td>

:所需的 output:

Brand name: Brand name 1
Brand distribution type: Authorised Reseller

我努力了:

for brand in brand_loop:
  print(brand.text)

但这不会打印开始<td>标记(“品牌名称 1”)之后的文本。

有什么建议么? 谢谢!

尝试

for brand in brand_loop:
  print(brand.text)
  print(brand.find('a').text)

您只能直接打印所选元素的文本。

您可以 select <td class="cell">然后.find_next(text=True)获取品牌名称,然后.find_next('a')获取品牌分布类型。

例如:

txt = '''<td class="cell">
 Brand Name 1
 <br/>
 (
 <a class="tip" title="This title">
  Authorised Resellers
 </a>
 )
</td>'''


soup = BeautifulSoup(txt, 'html.parser')

brand_name = soup.select_one('td.cell').find_next(text=True)
bran_distribution = brand_name.find_next('a').text

print('Brand name:', brand_name.strip())
print('Brand distribution type:', bran_distribution.strip())

印刷:

Brand name: Brand Name 1
Brand distribution type: Authorised Resellers

您可以使用find()next_element来获取第a td标记文本。而要简单地使用find()来获取标记文本。 你可以试试:

from bs4 import BeautifulSoup
html_doc = '''<td class="cell">
 Brand Name 1
 <br/>
 (
 <a class="tip" title="This title">
  Authorised Resellers
 </a>
 )
</td>'''

soup = BeautifulSoup(html_doc,'lxml')
brand_name = soup.find("td").next_element.strip()
brand_distribution_type = soup.find("a").text.strip()
print('Brand name:', brand_name)
print('Brand distribution type:', brand_distribution_type)

Output 将是:

Brand name: Brand Name 1
Brand distribution type: Authorised Resellers

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM