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抓取某汽车网数据解析html存入excel示例

python抓取某汽车网数据解析html存入excel示例

1、某汽车网站地址2、使用firefox查看后发现,此网站的信息未使用json数据,而是简单那的html页面而已 3、使用pyquery库中的PyQuery进行html的解析 页面样式:...

python访问抓取网页常用命令总结

python访问抓取网页常用命令 简单的抓取网页: import urllib.request url="http://google.cn/" response=urllib....

Python简单爬虫导出CSV文件的实例讲解

Python简单爬虫导出CSV文件的实例讲解

流程:模拟登录→获取Html页面→正则解析所有符合条件的行→逐一将符合条件的行的所有列存入到CSVData[]临时变量中→写入到CSV文件中 核心代码: ####写入Csv文件中...

python爬虫之BeautifulSoup 使用select方法详解

本文介绍了python爬虫之BeautifulSoup 使用select方法详解 ,分享给大家。具体如下: <html><head><title>...

python爬虫基础教程:requests库(二)代码实例

get请求 简单使用 import requests ''' 想要学习Python?Python学习交流群:973783996满足你的需求,资料都已经上传群文件,可以自行下载! ''...