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爬虫添加请求头代码实例

这篇文章主要介绍了python爬虫添加请求头代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 request import...

Python使用Beautiful Soup包编写爬虫时的一些关键点

1.善于利用soup节点的parent属性 比如对于已经得到了如下html代码: <td style="padding-left:0" width="60%"><l...

使用python爬虫获取黄金价格的核心代码

使用python爬虫获取黄金价格的核心代码

继续练手,根据之前获取汽油价格的方式获取了金价,暂时没钱投资,看看而已 #!/usr/bin/env python # -*- coding: utf-8 -*- """ 获取每天黄...

python爬虫面试宝典(常见问题)

是否了解线程的同步和异步? 线程同步:多个线程同时访问同一资源,等待资源访问结束,浪费时间,效率低 线程异步:在访问资源时在空闲等待时同时访问其他资源,实现多线程机制 是否...

python3实现爬取淘宝美食代码分享

python3实现爬取淘宝美食代码分享

环境: ubuntu16.04 python3.5 python库: selenium, pyquery,pymongo, re 要求: 设置×××面浏览器访问,并将商品列表存入mo...