符合中小企业对网站设计、功能常规化式的企业展示型网站建设
本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...
商城网站建设因基本功能的需求不同费用上面也有很大的差别...
手机微信网站开发、微信官网、微信商城网站...
import scrapy
from scrapy.linkextractors import LinkExtractor #导入url提取器的类
from scrapy.spiders import CrawlSpider, Rule #Rule可用于自动发送请求
class XuexiSpider(CrawlSpider):
name = 'xuexi'
allowed_domains = ['www.xxx.com']
start_urls = ['http://www.xxx.com/']
#LinkExtractor(allow=r'Items/') 该类的allow参数写入正则匹配规则,就会按照正则去响应信息中匹配URL,当然也有别的匹配规则,比如CSS
rules = (
#follow为True可以自动将所有响应信息的符合的规则的url都获取到,并发送请求
Rule(LinkExtractor(allow=r'Items/'), callback='parse_item', follow=True),
)
def parse_item(self, response):
item = {}
#item['domain_id'] = response.xpath('//input[@id="sid"]/@value').get()
#item['name'] = response.xpath('//div[@id="name"]').get()
#item['description'] = response.xpath('//div[@id="description"]').get()
return item
#1.爬虫文件.py代码示例:
import scrapy
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule
from sunPro.items import SunproItem,DetailItem
#需求:爬取sun网站中的编号,新闻标题,新闻内容,标号
class SunSpider(CrawlSpider):
name = 'sun'
# allowed_domains = ['www.xxx.com']
start_urls = ['http://wz.sun0769.com/index.php/question/questionType?type=4&page=']
#链接提取器:根据指定规则(allow="正则")进行指定链接的提取
link = LinkExtractor(allow=r'type=4&page=\d+')
link_detail = LinkExtractor(allow=r'question/\d+/\d+\.shtml')
rules = (
#规则解析器:将链接提取器提取到的链接进行指定规则(callback)的解析操作
Rule(link, callback='parse_item', follow=True),
#follow=True:可以将链接提取器 继续作用到 连接提取器提取到的链接 所对应的页面中
Rule(link_detail,callback='parse_detail')
)
#http://wz.sun0769.com/html/question//.shtml
#http://wz.sun0769.com/html/question//.shtml
#解析新闻编号和新闻的标题
#如下两个解析方法中是不可以实现请求传参!
#如法将两个解析方法解析的数据存储到同一个item中,可以以此存储到两个item
def parse_item(self, response):
#注意:xpath表达式中不可以出现tbody标签
tr_list = response.xpath('//*[@id="morelist"]/div/table[2]//tr/td/table//tr')
for tr in tr_list:
new_num = tr.xpath('./td[1]/text()').extract_first()
new_title = tr.xpath('./td[2]/a[2]/@title').extract_first()
item = SunproItem()
item['title'] = new_title
item['new_num'] = new_num
yield item
#解析新闻内容和新闻编号
def parse_detail(self,response):
new_id = response.xpath('/html/body/div[9]/table[1]//tr/td[2]/span[2]/text()').extract_first()
new_content = response.xpath('/html/body/div[9]/table[2]//tr[1]//text()').extract()
new_content = ''.join(new_content)
# print(new_id,new_content)
item = DetailItem()
item['content'] = new_content
item['new_id'] = new_id
yield item
#2.itmes.py代码示例:
#因为是不同页面的数据,又不能进行参数化,所有通过两个item类,来接收不同页面的解析数据
import scrapy
class SunproItem(scrapy.Item):
# define the fields for your item here like:
title = scrapy.Field()
new_num = scrapy.Field()
class DetailItem(scrapy.Item):
new_id = scrapy.Field()
content = scrapy.Field()
#3.pipeline.py代码示例:
#根据不同item的名字,来判断,数据来源于哪一个item
class SunproPipeline(object):
def process_item(self, item, spider):
#如何判定item的类型
#将数据写入数据库时,如何保证数据的一致性
if item.__class__.__name__ == 'DetailItem':
print(item['new_id'],item['content'])
pass
else:
print(item['new_num'],item['title'])
return item