2017-07-02 71 views
0

我需要從網頁上的下面的代碼使用Python和BeautifulSoup刮'文本'64%',請幫助。Python BeautifulSoup StyleTag提取

<span class="textword" style="width:64%">BUY</span> 

問候,babsdoc

+1

重複https://stackoverflow.com/questions/37843903/getting-style-of-tr-tag-using-beautifulsoup – TrakJohnson

+0

致謝!從您發佈TrakJohnson的帖子中得到答案! – babsdoc

+0

沒問題,在將來請求任何內容之前,請儘量[谷歌搜索的最低限度](https://www.google.fr/search?q=get+style+beautifulsoup) – TrakJohnson

回答

0
from bs4 import BeautifulSoup 
html='<span class="textword" style="width:64%">BUY</span>' 
soup=BeautifulSoup(html,'lxml') 

#if you have only one such element: 
needed_text = soup.find('span', {'style':'width:64%'}).text 

#if you have many elements to scrape: 
needed_text = [] 
needed_text_tags = soup.find_all('span', {'style':'width:64%'}) 
for i in needed_text_tags: 
    needed_text.append(i.text)