今天不详细讲代码逻辑,只讲如何入库
mongo的入库
from pyspider.libs.base_handler import *
from lxml import etree
import pymongo
class Handler(BaseHandler):
crawl_config = {
}
def __init__(self):
connection = pymongo.MongoClient(host='192.168.180.128',port=27017)
client = connection['lianjia']
self.db = client['items']
@every(minutes=24 * 60)
def on_start(self):
for i in range(1,101):
self.crawl('https://dg.lianjia.com/ershoufang/pg{}'.format(i), callback=self.index_page)
@config(age=10 * 24 * 60 * 60)
def index_page(self, response):
seletor = etree.HTML(response.text)
urls =seletor.xpath('//li[@class ="clear LOGCLICKDATA"]/a/@href')
for url in urls :
self.crawl(url, callback=se