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

yipeiwu_com4年前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爬虫给孩子起个好名字

利用Python爬虫给孩子起个好名字

前言 相信每位家长都有所体会,因为要在孩子出生后两周内起个名字(需要办理出生证明了),估计很多人都像我一样,刚开始是很慌乱的,虽然感觉汉字非常的多随便找个字做名字都行,后来才发现真不是随...

Python爬虫番外篇之Cookie和Session详解

Python爬虫番外篇之Cookie和Session详解

关于cookie和session估计很多程序员面试的时候都会被问到,这两个概念在写web以及爬虫中都会涉及,并且两者可能很多人直接回答也不好说的特别清楚,所以整理这样一篇文章,也帮助自己...

python网络爬虫之如何伪装逃过反爬虫程序的方法

有的时候,我们本来写得好好的爬虫代码,之前还运行得Ok, 一下子突然报错了。 报错信息如下: Http 800 Internal internet error 这是因为你的对象网站设置了...

Python爬虫框架Scrapy基本用法入门教程

Python爬虫框架Scrapy基本用法入门教程

本文实例讲述了Python爬虫框架Scrapy基本用法。分享给大家供大家参考,具体如下: Xpath <html> <head> <title>...

python抓取多种类型的页面方法实例

与抓取预定义好的页面集合不同,抓取一个网站的所有内链会带来一个 挑战,即你不知道会获得什么。好在有几种基本的方法可以识别页面类型。 通过URL 一个网站中所有的博客文章可能都会包含一...