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程序设计有所帮助。

相关文章

利用PyCharm Profile分析异步爬虫效率详解

利用PyCharm Profile分析异步爬虫效率详解

今天比较忙,水一下 下面的代码来源于这个视频里面提到的,github 的链接为:github.com/mikeckenned…(本地下载) 第一个代码如下,就是一个普通的 for 循环...

三个python爬虫项目实例代码

这篇文章主要介绍了三个python爬虫项目实例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 爬取内涵段子: #encodi...

python 写的一个爬虫程序源码

写爬虫是一项复杂、枯噪、反复的工作,考虑的问题包括采集效率、链路异常处理、数据质量(与站点编码规范关系很大)等。整理自己写一个爬虫程序,单台服务器可以启用1~8个实例同时采集,然后将数据...

python爬虫之模拟登陆csdn的实例代码

python模拟登陆网页主要使用到urllib、urllib2、cookielib及BeautifulSoup等基本模块,当然进阶阶段我们还可以使用像requests等更高级一点的模块。...

Python实现抓取城市的PM2.5浓度和排名

Python实现抓取城市的PM2.5浓度和排名

主机环境:(Python2.7.9 / Win8_64 / bs4) 利用BeautifulSoup4来抓取 www.pm25.com 上的PM2.5数据,之所以抓取这个网站,是因为上面...