Python使用scrapy抓取网站sitemap信息的方法
更新时间:2015年04月08日 09:46:40 作者:pythoner
这篇文章主要介绍了Python使用scrapy抓取网站sitemap信息的方法,涉及Python框架scrapy的使用技巧,具有一定参考借鉴价值,需要的朋友可以参考下
本文实例讲述了Python使用scrapy抓取网站sitemap信息的方法。分享给大家供大家参考。具体如下:
import re from scrapy.spider import BaseSpider from scrapy import log from scrapy.utils.response import body_or_str from scrapy.http import Request from scrapy.selector import HtmlXPathSelector class SitemapSpider(BaseSpider): name = "SitemapSpider" start_urls = ["http://www.domain.com/sitemap.xml"] def parse(self, response): nodename = 'loc' text = body_or_str(response) r = re.compile(r"(<%s[\s>])(.*?)(</%s>)"%(nodename,nodename),re.DOTALL) for match in r.finditer(text): url = match.group(2) yield Request(url, callback=self.parse_page) def parse_page(self, response): hxs = HtmlXPathSelector(response) #Mock Item blah = Item() #Do all your page parsing and selecting the elemtents you want blash.divText = hxs.select('//div/text()').extract()[0] yield blah
希望本文所述对大家的Python程序设计有所帮助。
相关文章
PyInstaller将Python脚本打包为.exe可执行文件的步骤详解
Python是一门强大而灵活的编程语言,为了方便共享和部署,我们可以将 Python 脚本打包为可执行文件(.exe),这样其他用户就无需安装 Python环境,直接运行可执行文件即可,本文将介绍如何使用PyInstaller 工具实现这一目标,需要的朋友可以参考下2023-12-12Python实现图片裁剪的两种方式(Pillow和OpenCV)
这篇文章主要介绍了Python实现图片裁剪的两种方式(Pillow和OpenCV),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2019-10-10pytorch1.0中torch.nn.Conv2d用法详解
今天小编就为大家分享一篇pytorch1.0中torch.nn.Conv2d用法详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧2020-01-01
最新评论