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程序设计有所帮助。
相关文章
jupyter notebook 自定义python解释器的过程详解
大家都知道jupyter notebook 网页版交互环境,类似于ipython,功能强大,这篇文章主要介绍了jupyter notebook 自定义python解释器的过程,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2022-10-10


最新评论