通过python抓取sitemap.xml
我正在抓取一个sitemap.xml,我的目标是找到所有的url以及它们的增量计数 下面是xml的结构通过python抓取sitemap.xml,python,xml,beautifulsoup,web-crawler,Python,Xml,Beautifulsoup,Web Crawler,我正在抓取一个sitemap.xml,我的目标是找到所有的url以及它们的增量计数 下面是xml的结构 <?xml version="1.0" encoding="UTF-8"?> <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> <url> <loc>http://www.htcysnc.com/m/designer-sarees</loc>
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>http://www.htcysnc.com/m/designer-sarees</loc>
<lastmod>2014-09-01</lastmod>
<changefreq>hourly</changefreq>
<priority>0.9</priority>
</url>
<url>
<loc>http://www.htcysnc.com/m/anarkali-suits</loc>
<lastmod>2014-09-01</lastmod>
<changefreq>hourly</changefreq>
<priority>0.9</priority>
</url>
我的输出是这样的
<loc>http://www.htcysnc.com/elegant-yellow-green-suit-seven-east-p63703</loc>
1
<loc>http://www.htcysnc.com/elegant-orange-pink-printed-suit-seven-east-p63705</loc>
2
http://www.htcysnc.com/elegant-yellow-green-suit-seven-east-p63703
1.
http://www.htcysnc.com/elegant-orange-pink-printed-suit-seven-east-p63705
2.
需要输出为不带loc和/loc的链接。已尝试使用replace命令,但出现了错误。此处,
要查找的项列表中的每个项都是标记
类型对象,因此您可以使用.text
或.string
获取标记中的字符串。虽然.text
和.string
都具有相同的功能,但在本例中,这两种功能的作用相同
for loc in item_to_be_found:
print item_to_be_found.index(loc) + 1, loc.text
这将给你一个类似的结果
1 http://www.htcysnc.com/m/designer-sarees
2 http://www.htcysnc.com/m/anarkali-suits
您可以使用一些属性来代替循环,这样可以使代码更快一些
print i.text.strip()
这将为您提供不带任何标签的必要信息。您有什么问题吗?抱歉,帖子搞乱了。我现在已经编辑了。为什么我一直没有定义loc?
print i.text.strip()