Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentes Révision précédente | |||
python:xml [2023/09/16 13:58] marclebrun |
python:xml [2023/09/16 14:08] (Version actuelle) marclebrun |
||
---|---|---|---|
Ligne 32: | Ligne 32: | ||
<code bash> | <code bash> | ||
pip install lxml | pip install lxml | ||
+ | </code> | ||
+ | |||
+ | ===== Utiliser BeautifulSoup4 ===== | ||
+ | |||
+ | <code bash> | ||
+ | pip install beautifulsoup4 | ||
+ | </code> | ||
+ | |||
+ | <code python> | ||
+ | from bs4 import BeautifulSoup | ||
+ | import requests | ||
+ | |||
+ | xmlDict = {} | ||
+ | |||
+ | r = requests.get("http://www.site.co.uk/sitemap.xml") | ||
+ | xml = r.text | ||
+ | |||
+ | soup = BeautifulSoup(xml) | ||
+ | sitemapTags = soup.find_all("sitemap") | ||
+ | |||
+ | print "The number of sitemaps are {0}".format(len(sitemapTags)) | ||
+ | |||
+ | for sitemap in sitemapTags: | ||
+ | xmlDict[sitemap.findNext("loc").text] = sitemap.findNext("lastmod").text | ||
+ | |||
+ | print xmlDict | ||
</code> | </code> | ||