Python使用scrapy抓取网站sitemap信息的方法

yipeiwu_com5年前Python爬虫

本文实例讲述了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程序设计有所帮助。

相关文章

Python爬虫beautifulsoup4常用的解析方法总结

摘要 如何用beautifulsoup4解析各种情况的网页 beautifulsoup4的使用 关于beautifulsoup4,官网已经讲的很详细了,我这里就把一些常用的解析方...

python爬虫超时的处理的实例

如下所示: #coding:utf-8 ''''' Created on 2014-7-24 @author: Administrator ''' import url...

Python代理IP爬虫的新手使用教程

Python代理IP爬虫的新手使用教程

前言 Python爬虫要经历爬虫、爬虫被限制、爬虫反限制的过程。当然后续还要网页爬虫限制优化,爬虫再反限制的一系列道高一尺魔高一丈的过程。爬虫的初级阶段,添加headers和ip代理可...

Python3爬虫学习之MySQL数据库存储爬取的信息详解

Python3爬虫学习之MySQL数据库存储爬取的信息详解

本文实例讲述了Python3爬虫学习之MySQL数据库存储爬取的信息。分享给大家供大家参考,具体如下: 数据库存储爬取的信息(MySQL) 爬取到的数据为了更好地进行分析利用,而之前将爬...

Python实现简单的获取图片爬虫功能示例

本文实例讲述了Python实现简单的获取图片爬虫功能。分享给大家供大家参考,具体如下: 简单Python爬虫,获得网页上的照片 #coding=utf-8 import urllib...