时间:2021-07-01 10:21:17 帮助过:17人阅读
{
"_id" : ObjectId("5577ae44745d785e65fa8686"),
"from_url" : "http://tech.163.com/",
"news_body" : [
"科技讯 6月9日凌晨消息2015",
"全球开发人员大会(WWDC 2015)在旧",
"召开,网易科技进行了全程图文直播。最新",
"9操作系统在",
"上性能得到极大提升,能够实现分屏显示。也能够支持画中画功能。",
"新版iOS 9 添加了QuickType 键盘,让输入和编辑都更简单快捷。在搭配外置键盘使用 iPad 时。用户能够用快捷键来进行操作,比如在不同 app 之间进行切换。",
"并且。iOS 9 又一次设计了 app 间的切换。iPad的分屏功能能够让用户在不离开当前 app 的同一时候就能打开第二个 app。这意味着两个app在同一屏幕上。同一时候开启、并行运作。
两个屏幕的比例能够是5:5,也能够是7:3。",
"另外,iPad还支持“画中画”功能,能够将正在播放的视频缩放到一角,然后利用屏幕其他空间处理其他的工作。
",
"据透露分屏功能仅仅支持iPad Air2。画中画功能将仅仅支持iPad Air, iPad Air2, iPad mini2, iPad mini3。",
"\r\n"
],
"news_from" : "网易科技报道",
"news_thread" : "ARKR2G22000915BD",
"news_time" : "2015-06-09 02:24:55",
"news_title" : "iOS 9在iPad上可实现分屏功能",
"news_url" : "http://tech.163.com/15/0609/02/ARKR2G22000915BD.html"
}
以下就是须要改动的文件:1.spider 爬虫文件,制定抓取规则主要是利用xpath
2.items.py 主要指定抓取的内容
3.pipeline.py 有一个指向和存储数据的功能。这里我们还会添加一个store.py的文件。文件内部就是创建一个MongoDB的数据库。
4.setting.py 配置文件,主要是配置代理、User_Agent、抓取时间间隔、延时等等
主要就是这几个文件,这个scrapy照曾经的爬虫我添加了几个新功能,一个是和数据库链接实现存储的功能。不在是存成json或者txt文件。第二个就是在spider中设置了follow = True这个属性,意思就是在爬到的结果上继续往下爬,相当于一个深搜的过程。以下我们看看源码。
一般首先我们写的是items.py文件
# -*- coding: utf-8 -*-
import scrapy
class Tech163Item(scrapy.Item):
news_thread = scrapy.Field()
news_title = scrapy.Field()
news_url = scrapy.Field()
news_time = scrapy.Field()
news_from = scrapy.Field()
from_url = scrapy.Field()
news_body = scrapy.Field()
之后我们编写的就是spider文件。我们能够随便命名一个文件,由于我们调用爬虫的时候仅仅需知道它文件内部的爬虫名字就能够了,也就是name = "news"这个属性。我们这里的爬虫名字叫做news。假设你须要使用这个爬虫你可能须要改动下面Rule里的allow属性,改动一下时间。由于网易新闻不会存储超过一年时间的新闻。你能够将时间改为最近假设如今为15年8月你就能够改动为/15/08。
#encoding:utf-8
import scrapy
import re
from scrapy.selector import Selector
from tech163.items import Tech163Item
from scrapy.contrib.linkextractors import LinkExtractor
from scrapy.contrib.spiders import CrawlSpider,Rule
class Spider(CrawlSpider):
name = "news"
allowed_domains = ["tech.163.com"]
start_urls = [‘http://tech.163.com/‘]
rules = (
Rule(
LinkExtractor(allow = r"/15/06\d+/\d+/*"),
#代码中的正则/15/06\d+/\d+/*的含义是大概是爬去/15/06开头而且后面是数字/数字/不论什么格式/的新闻
callback = "parse_news",
follow = True
#follow=ture定义了是否再爬到的结果上继续往后爬
),
)
def parse_news(self,response):
item = Tech163Item()
item[‘news_thread‘] = response.url.strip().split(‘/‘)[-1][:-5]
self.get_title(response,item)
self.get_source(response,item)
self.get_url(response,item)
self.get_news_from(response,item)
self.get_from_url(response,item)
self.get_text(response,item)
return item
def get_title(self,response,item):
title = response.xpath("/html/head/title/text()").extract()
if title:
item[‘news_title‘] = title[0][:-5]
def get_source(self,response,item):
source = response.xpath("//div[@class=‘ep-time-soure cDGray‘]/text()").extract()
if source:
item[‘news_time‘] = source[0][9:-5]
def get_news_from(self,response,item):
news_from = response.xpath("//div[@class=‘ep-time-soure cDGray‘]/a/text()").extract()
if news_from:
item[‘news_from‘] = news_from[0]
def get_from_url(self,response,item):
from_url = response.xpath("//div[@class=‘ep-time-soure cDGray‘]/a/@href").extract()
if from_url:
item[‘from_url‘] = from_url[0]
def get_text(self,response,item):
news_body = response.xpath("//div[@id=‘endText‘]/p/text()").extract()
if news_body:
item[‘news_body‘] = news_body
def get_url(self,response,item):
news_url = response.url
if news_url:
item[‘news_url‘] = news_url
import pymongo import random HOST = "127.0.0.1" PORT = 27017 client = pymongo.MongoClient(HOST,PORT) NewsDB = client.NewsDB
from store import NewsDB
class Tech163Pipeline(object):
def process_item(self, item, spider):
if spider.name != "news":
return item
if item.get("news_thread",None) is None:
return item
spec = {"news_thread":item["news_thread"]}
NewsDB.new.update(spec,{"$set":dict(item)},upsert = True)
return None
BOT_NAME = ‘tech163‘ SPIDER_MODULES = [‘tech163.spiders‘] NEWSPIDER_MODULE = ‘tech163.spiders‘ ITEM_PIPELINES = [‘tech163.pipelines.Tech163Pipeline‘,] # Crawl responsibly by identifying yourself (and your website) on the user-agent #USER_AGENT = ‘tech163 (+http://www.yourdomain.com)‘ USER_AGENT = ‘Mozilla/5.0 (X11; Linux x86_64; rv:7.0.1) Gecko/20100101 Firefox/7.7‘ DOWNLOAD_TIMEOUT = 15
利用scrapy抓取网易新闻并将其存储在mongoDB
标签:开头 tin pad com dom rip 使用 大会 sql