简体   繁体   English

从 JSON 中提取粗体文本

[英]Extract Bold Text from JSON

I'm using Google API and GSC to get the bold from the htmlSnippet:我正在使用 Google API 和 GSC 从 htmlSnippet 中获取粗体:

from apiclient.discovery import build
from bs4 import BeautifulSoup

search_term="search term in Google"

api_key=""

resource=build("customsearch", 'v1', developerKey=api_key).cse()

result=resource.list(q=search_term,cx=' ').execute()

for i in result['items']:
    html=str(i['htmlSnippet'])
    print(html)

So I get something like this:所以我得到这样的东西:

Metadescription from Google in <b>bolds text</b>. Here there is <b>another bold</b>

Then I try with this:然后我试试这个:

soup=BeautifulSoup(html,"lxml")
    print(soup.find_all('b'))

And it works but I can't get only the text.它有效,但我不能只得到文本。

Trying with:尝试:

soup=BeautifulSoup(html,"lxml")
    print(soup.find_all('b').text)

Doesn't work =/不起作用=/

Pleas help me!请帮助我!

soup.find_all()返回一个列表,您需要在循环中获取每个列表的文本。

print(b.text for b in soup.find_all('b'))

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

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