2017-07-02 73 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)