|
1 |
| -from pysitemaps import Sitemap, Url |
| 1 | +from pysitemaps import Sitemap, Url, XmlDocument |
| 2 | +from pprint import pprint |
2 | 3 |
|
3 |
| -if __name__ == "__main__": |
4 | 4 |
|
5 |
| - smp = Sitemap() |
6 |
| - smp.add_url(Url("a.html", "2022", ["abc.png"])) |
7 |
| - smp.add_url(Url("b.html", "2021", ["def.png"])) |
8 |
| - smp.add_url(Url("b.html", "2021", ["c.png", "b.png"])) |
9 |
| - smp.process() |
| 5 | +def demo_read_sitemap(): |
| 6 | + smp = Sitemap( |
| 7 | + website_name="seowings.org", |
| 8 | + ) |
| 9 | + smp.read("sitemap.xml") |
| 10 | + pprint(smp.as_dict()) |
| 11 | + |
| 12 | + |
| 13 | +def demo_create_sitemap(): |
| 14 | + smp = Sitemap( |
| 15 | + website_name="https://www.seowings.org/", |
| 16 | + file_path="sitemap.xml", |
| 17 | + xsl_file="https://www.seowings.org/main-sitemap.xsl", |
| 18 | + ) |
| 19 | + smp.append( |
| 20 | + Url( |
| 21 | + loc="https://www.seowings.org/a.html", |
| 22 | + lastmod="2022-12-25", |
| 23 | + images_loc=["https://www.seowings.org/a1.png"], |
| 24 | + ) |
| 25 | + ) |
| 26 | + smp.append( |
| 27 | + { |
| 28 | + "loc": "https://www.seowings.org/b.html", |
| 29 | + "lastmod": "2023-05-01", |
| 30 | + "images_loc": [ |
| 31 | + "https://www.seowings.org/b1.png", |
| 32 | + "https://www.seowings.org/b2.png", |
| 33 | + ], |
| 34 | + } |
| 35 | + ) |
10 | 36 | smp.write()
|
| 37 | + pprint(smp.as_dict()) |
| 38 | + |
| 39 | + |
| 40 | +def demo_locate_sitemap(): |
| 41 | + smp = Sitemap(website_name="https://www.seowings.org/") |
| 42 | + smp.fetch(include_urls=False) |
| 43 | + print(smp.as_dict()) |
| 44 | + |
| 45 | + |
| 46 | +def demo_fetch_sitemap(): |
| 47 | + smp = Sitemap(website_name="https://www.seowings.org/") |
| 48 | + smp.fetch(include_urls=True) |
| 49 | + print(smp.as_dict()) |
| 50 | + |
| 51 | + |
| 52 | +def demo_fetch_index_sitemap(): |
| 53 | + smp = Sitemap(website_name="https://www.dw.com/") |
| 54 | + smp.fetch(include_urls=False) |
| 55 | + print(smp.as_dict()) |
| 56 | + |
| 57 | + |
| 58 | +def demo_create_xml_doc(): |
| 59 | + from datetime import datetime |
| 60 | + |
| 61 | + news_sitemap = XmlDocument( |
| 62 | + "sitemap-news.xml", |
| 63 | + lastmod=datetime.now().strftime("%Y-%m-%d"), |
| 64 | + include_urls=False, |
| 65 | + ) |
| 66 | + news_sitemap.add_object(Url("b.html", "2023-05-02", ["img1.png", "img2.png"])) |
| 67 | + news_sitemap.add_url( |
| 68 | + loc="c.html", lastmod="2023-01-02", images_loc=["img4.png", "img5.png"] |
| 69 | + ) |
| 70 | + news_sitemap.add_from_text( |
| 71 | + """ |
| 72 | + <urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> |
| 73 | + <url> |
| 74 | + <loc>z.html</loc> |
| 75 | + <lastmod>2022</lastmod> |
| 76 | + <image:image> |
| 77 | + <image:loc>z.png</image:loc> |
| 78 | + </image:image> |
| 79 | + </url> |
| 80 | + <url> |
| 81 | + <loc>dz.html</loc> |
| 82 | + <lastmod>2022</lastmod> |
| 83 | + <image:image> |
| 84 | + <image:loc>z.png</image:loc> |
| 85 | + </image:image> |
| 86 | + <image:image> |
| 87 | + <image:loc>a.png</image:loc> |
| 88 | + </image:image> |
| 89 | + </url> |
| 90 | + </urlset> |
| 91 | + """ |
| 92 | + ) |
| 93 | + |
| 94 | + print(news_sitemap.as_dict()) |
| 95 | + |
| 96 | + |
| 97 | +if __name__ == "__main__": |
| 98 | + demo_create_sitemap() |
| 99 | + demo_read_sitemap() |
| 100 | + demo_locate_sitemap() |
| 101 | + demo_fetch_sitemap() |
| 102 | + demo_fetch_index_sitemap() |
| 103 | + demo_create_xml_doc() |
0 commit comments