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

yipeiwu_com7年前Python爬虫

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

Xpath

<html>
<head>
  <title>标题</title>
</head>
<body>
  <h2>二级标题</h2>
  <p>爬虫1</p>
  <p>爬虫2</p>
</body>
</html>

在上述html代码中,我要获取h2的内容,我们可以使用以下代码进行获取:

info = response.xpath("/html/body/h2/text()")

可以看出/html/body/h2为内容的层次结构,text()则是获取h2标签的内容。//p获取所有p标签。获取带具体属性的标签://标签[@属性="属性值"]

<div class="hide"></div>

获取class为hide的div标签

div[@class="hide"]

再比如,我们在谷歌Chrome浏览器上的Console界面使用$x['//h2']命令获取页面中的h2元素信息:

xmlfeed模板

创建一个xmlfeed模板的爬虫

scrapy genspider -t xmlfeed abc iqianyue.com

核心代码:

from scrapy.spiders import XMLFeedSpider
class AbcSpider(XMLFeedSpider):
  name = 'abc'
  start_urls = ['http://yum.iqianyue.com/weisuenbook/pyspd/part12/test.xml']
  iterator = 'iternodes' # 迭代器,默认为iternodes,是一个基于正则表达式的高性能迭代器。除了iternodes,还有“html”和“xml”
  itertag = 'person' # 设置从哪个节点(标签)开始迭代
  # parse_node会在节点与提供的标签名相符时自动调用
  def parse_node(self, response, selector):
    i = {}
    xpath = "/person/email/text()"
    info = selector.xpath(xpath).extract()
    print(info)
    return i

csvfeed模板

创建一个csvfeed模板的爬虫

scrapy genspider -t csvfeed csvspider iqianyue.com

核心代码

from scrapy.spiders import CSVFeedSpider
class CsvspiderSpider(CSVFeedSpider):
  name = 'csvspider'
  allowed_domains = ['iqianyue.com']
  start_urls = ['http://yum.iqianyue.com/weisuenbook/pyspd/part12/mydata.csv']
  # headers 主要存放csv文件中包含的用于提取字段的信息列表
  headers = ['name', 'sex', 'addr', 'email']
  # delimiter 字段之间的间隔
  delimiter = ','
  def parse_row(self, response, row):
    i = {}
    name = row["name"]
    sex = row["sex"]
    addr = row["addr"]
    email = row["email"]
    print(name,sex,addr,email)
    #i['url'] = row['url']
    #i['name'] = row['name']
    #i['description'] = row['description']
    return i

crawlfeed模板

创建一个crawlfeed模板的爬虫

scrapy genspider -t crawlfeed crawlspider sohu.com

核心代码

class CrawlspiderSpider(CrawlSpider):
  name = 'crawlspider'
  allowed_domains = ['sohu.com']
  start_urls = ['http://sohu.com/']
  rules = (
    Rule(LinkExtractor(allow=r'Items/'), callback='parse_item', follow=True),
  )
  def parse_item(self, response):
    i = {}
    #i['domain_id'] = response.xpath('//input[@id="sid"]/@value').extract()
    #i['name'] = response.xpath('//div[@id="name"]').extract()
    #i['description'] = response.xpath('//div[@id="description"]').extract()
    return i

上面代码rules部分中的LinkExtractor为连接提取器。

LinkExtractor中对应的参数及含义

参数名 参数含义
allow 提取符合正则表达式的链接
deny 不提取符合正则表达式的链接
restrict_xpaths 使用XPath表达式与allow共同作用提取同时符合对应XPath表达式和对应正则表达式的链接
allow_domains 允许提取的域名,比如我们想只提取某个域名下的链接时会用到
deny_domains 进制提取的域名

更多关于Python相关内容可查看本站专题:《Python Socket编程技巧总结》、《Python正则表达式用法总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总

希望本文所述对大家Python程序设计有所帮助。

相关文章

Python3爬虫学习之爬虫利器Beautiful Soup用法分析

Python3爬虫学习之爬虫利器Beautiful Soup用法分析

本文实例讲述了Python3爬虫学习之爬虫利器Beautiful Soup用法。分享给大家供大家参考,具体如下: 爬虫利器Beautiful Soup 前面一篇说到通过urllib.re...

Python 爬取携程所有机票的实例代码

Python 爬取携程所有机票的实例代码

打开携程网,查询机票,如广州到成都。 这时网址为:http://flights.ctrip.com/booking/CAN-CTU-day-1.html?DDate1=2018-06-1...

python爬虫selenium和phantomJs使用方法解析

python爬虫selenium和phantomJs使用方法解析

1.selenum:三方库。可以实现让浏览器完成自动化的操作。 2.环境搭建 2.1 安装: pip install selenium 2.2 获取浏览器的驱动程序 下载地址...

Python爬虫代理IP池实现方法

Python爬虫代理IP池实现方法

在公司做分布式深网爬虫,搭建了一套稳定的代理池服务,为上千个爬虫提供有效的代理,保证各个爬虫拿到的都是对应网站有效的代理IP,从而保证爬虫快速稳定的运行,当然在公司做的东...

使用Python抓取模板之家的CSS模板

使用Python抓取模板之家的CSS模板

Python版本是2.7.9,在win8上测试成功,就是抓取有点慢,本来想用多线程的,有事就罢了。模板之家的网站上的url参数与页数不匹配,懒得去做分析了,就自己改代码中的url吧。大神...