簡體   English   中英

從 JSON 中提取粗體文本

[英]Extract Bold Text from JSON

我正在使用 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)

所以我得到這樣的東西:

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

然后我試試這個:

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

它有效,但我不能只得到文本。

嘗試:

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

不起作用=/

請幫助我!

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