You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
74 lines
3.6 KiB
74 lines
3.6 KiB
import scrapy
|
|
from uuid import uuid4
|
|
import re
|
|
from scrapy.spiders import CrawlSpider
|
|
from demo1.custom_settings_conf import *
|
|
from demo1.items import Shouyelianjie
|
|
from scrapy.utils.project import get_project_settings
|
|
import pymysql
|
|
import logging
|
|
import json
|
|
import time
|
|
from lxml import etree
|
|
from urllib import parse
|
|
from demo1.Util import Util_WANG
|
|
class qicetong(scrapy.Spider,Util_WANG):
|
|
name = 'qicetongspider'
|
|
settings = get_project_settings()
|
|
allowed_domains = ['easyshb.com']
|
|
custom_settings = custom_settings_conf_qicetongSpider
|
|
start_urls=['http://www.easyshb.com/alias/zck.htm?categoryId=&name=&areaLevel=&cityId=¬iceType=M7EkvSokQa3QVgX6WFf5LP&policyType=']
|
|
#start_urls=['http://www.easyshb.com/alias/zck.htm?categoryId=&name=&pageNumber=76&areaLevel=&cityId=¬iceType=M7EkvSokQa3QVgX6WFf5LP&policyType=']
|
|
def __init__(self, name=None, **kwargs):
|
|
self.db = pymysql.connect(
|
|
host=self.settings['MYSQL_HOST'],
|
|
database=self.settings['MYSQL_DATABASE'],
|
|
user=self.settings['MYSQL_USER'],
|
|
password=self.settings['MYSQL_PASSWORD'],
|
|
port=3306,
|
|
charset='utf8',
|
|
cursorclass=pymysql.cursors.DictCursor,
|
|
use_unicode=True
|
|
)
|
|
self.cursor = self.db.cursor()
|
|
def parse(self, response):
|
|
a_lis=response.xpath('//div[contains(@class,"list_flex")]/a')
|
|
for a_lis_sign in a_lis:
|
|
item=Shouyelianjie()
|
|
item['diqu']=1000008
|
|
item['biaoti']=a_lis_sign.xpath('.//h3/text()').extract_first()
|
|
item['shijian']=a_lis_sign.xpath('.//p[@class="f14 fc-gray mt-5"]/text()').extract_first()
|
|
item['jianjie']=a_lis_sign.xpath('.//p[@class="ellipsis3 fc-gray mt-10"]//text()').extract_first()
|
|
item['laiyuan']='来源未知'
|
|
item['lianjie']=response.urljoin(a_lis_sign.xpath('./@href').extract_first())
|
|
if not self.settings.get("ISQUANPA"):
|
|
self.cursor.execute(
|
|
'select count(title_url) as nums FROM t_policy where title_url ="{}"'.format(item['lianjie']))
|
|
res = self.cursor.fetchall()[0].get('nums')
|
|
if res == 0:
|
|
yield scrapy.Request(url=item['lianjie'], meta={'item': item}, callback=self.page_url)
|
|
else:
|
|
logging.info('这个链接已经爬过了-----:' + item['lianjie'])
|
|
else:
|
|
yield scrapy.Request(url=item['lianjie'], meta={'item': item}, callback=self.page_url)
|
|
if self.settings.get("ISQUANPA"):
|
|
try:
|
|
next_page_num = response.xpath('//script[contains(text(),"laypage")]').re("pages.*?[\s\S]*?curr.*?\"\d*\"?")[0]
|
|
s=re.findall('\d+',next_page_num)
|
|
pages=int(s[0])
|
|
curr=int(s[1])
|
|
if pages>curr:
|
|
next_page='http://www.easyshb.com/alias/zck.htm?categoryId=&name=&pageNumber='+str(curr+1)+'&areaLevel=&cityId=¬iceType=M7EkvSokQa3QVgX6WFf5LP&policyType='
|
|
yield scrapy.Request(url=next_page, callback=self.parse)
|
|
except:
|
|
logging.info('全部爬取完毕')
|
|
def page_url(self,response):
|
|
item=response.meta['item']
|
|
item['xiangqing'] = response.xpath('//div[contains(@class,"content")]').extract_first()
|
|
self.tihuan_a_return(item, self.settings.get('FILE_PATH'), response)
|
|
self.tihuan_img_return(item, self.settings.get('MESSAGE'), response)
|
|
yield item
|
|
def a_fun(self,href):
|
|
pass
|
|
def img_fun(self, src):
|
|
pass
|