简体   繁体   English

使用 Beautiful Soup 从 HTML 标签内部提取数据

[英]Pulling data from inside HTML tag using Beautiful Soup

I am trying to grab the information from the HTML code below with BeautifulSoup.我正在尝试使用 BeautifulSoup 从下面的 HTML 代码中获取信息。

I'm trying to grab the information in d= .我正在尝试获取d=中的信息。

I tried using我尝试使用

for node in soup.findAll('d'):
    print(''.join(node.findAll(text=True)))

However it did not return any information.但是它没有返回任何信息。

Any help would be appreciated.任何帮助,将不胜感激。

<path fill="none" stroke="#F39C13" stroke-width="3" d="M70,398L80.91495058333334,398L80.91495058333334,393.12462491393086L139.9525775,393.12462491393086L139.9525775,390.3995549380779L171.26091083333333,390.3995549380779L171.26091083333333,375.6214167892933L191.91833333333335,375.6214167892933L191.91833333333335,370.57940817475315L192.15775,370.57940817475315L192.15775,346.8216070234775L192.52608333333333,346.8216070234775L192.52608333333333,316.5126268923282L193.31799999999998,316.5126268923282L193.31799999999998,311.17636059522624L195.11049416666665,311.17636059522624L195.11049416666665,294.184572994709L250.7719225,294.184572994709L250.7719225,289.64175873696803L251.5575775,289.64175873696803L251.5575775,284.19552891552445L278.176475,284.19552891552445L278.176475,274.12735109627243L317.6176083333333,274.12735109627243L317.6176083333333,267.6450061351061L361.59108333333336,267.6450061351061L361.59108333333336,261.7097197920084L376.50305833333334,261.7097197920084L376.50305833333334,257.4876501142978L398.179475,257.4876501142978L398.179475,253.6807437062263L413.20010833333333,253.6807437062263L413.20010833333333,244.68390284180637L415.8889416666667,244.68390284180637L415.8889416666667,232.59131014943122L433.19508333333334,232.59131014943122L433.19508333333334,217.11191560626028L462.73541666666665,217.11191560626028L462.73541666666665,212.86778950989245L537.1074416666668,212.86778950989245L537.1074416666668,181.49502268135544L572.4932249999999,181.49502268135544L572.4932249999999,153.38834724724293L579.8101666666666,153.38834724724293L579.8101666666666,147.33719431459903L585.7403333333334,147.33719431459903L585.7403333333334,132.7568776518106L628.0489416666667,132.7568776518106L628.0489416666667,127.66153735816481L632.008525,127.66153735816481L632.008525,120.59249015748499L637.743475,120.59249015748499L637.743475,112.57549162805078L680.1441666666666,112.57549162805078L680.1441666666666,107.87408149242401L696.3931916666667,107.87408149242401L696.3931916666667,104.2686095237732L704.7727749999999,104.2686095237732L704.7727749999999,102.36035726070402L733,102.36035726070402L733,102.36035726070402"></path>

How about the following下面怎么样

from bs4 import BeautifulSoup

html = """<path fill="none" stroke="#F39C13" stroke-width="3" d="M70,398L80.91495058333334,398L80.91495058333334,
393.12462491393086L139.9525775,393.12462491393086L139.9525775,390.3995549380779L171.26091083333333,
390.3995549380779L171.26091083333333,375.6214167892933L191.91833333333335,375.6214167892933L191.91833333333335,
370.57940817475315L192.15775,370.57940817475315L192.15775,346.8216070234775L192.52608333333333,
346.8216070234775L192.52608333333333,316.5126268923282L193.31799999999998,316.5126268923282L193.31799999999998,
311.17636059522624L195.11049416666665,311.17636059522624L195.11049416666665,294.184572994709L250.7719225,
294.184572994709L250.7719225,289.64175873696803L251.5575775,289.64175873696803L251.5575775,
284.19552891552445L278.176475,284.19552891552445L278.176475,274.12735109627243L317.6176083333333,
274.12735109627243L317.6176083333333,267.6450061351061L361.59108333333336,267.6450061351061L361.59108333333336,
261.7097197920084L376.50305833333334,261.7097197920084L376.50305833333334,257.4876501142978L398.179475,
257.4876501142978L398.179475,253.6807437062263L413.20010833333333,253.6807437062263L413.20010833333333,
244.68390284180637L415.8889416666667,244.68390284180637L415.8889416666667,232.59131014943122L433.19508333333334,
232.59131014943122L433.19508333333334,217.11191560626028L462.73541666666665,217.11191560626028L462.73541666666665,
212.86778950989245L537.1074416666668,212.86778950989245L537.1074416666668,181.49502268135544L572.4932249999999,
181.49502268135544L572.4932249999999,153.38834724724293L579.8101666666666,153.38834724724293L579.8101666666666,
147.33719431459903L585.7403333333334,147.33719431459903L585.7403333333334,132.7568776518106L628.0489416666667,
132.7568776518106L628.0489416666667,127.66153735816481L632.008525,127.66153735816481L632.008525,
120.59249015748499L637.743475,120.59249015748499L637.743475,112.57549162805078L680.1441666666666,
112.57549162805078L680.1441666666666,107.87408149242401L696.3931916666667,107.87408149242401L696.3931916666667,
104.2686095237732L704.7727749999999,104.2686095237732L704.7727749999999,102.36035726070402L733,
102.36035726070402L733,102.36035726070402"></path> """

soup = BeautifulSoup(html, features="lxml")
element = soup.select('path:nth-child(1)')[0]['d']
print(element)

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

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