国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

python - 用scrapy爬取網(wǎng)站內(nèi)容時(shí),如何忽略某些內(nèi)容為空的值;

瀏覽:110日期:2022-06-28 13:59:25

問(wèn)題描述

我爬取京東上所有手機(jī)信息時(shí)會(huì)碰到如下問(wèn)題:1、返回值過(guò)多,如下圖片所示:python - 用scrapy爬取網(wǎng)站內(nèi)容時(shí),如何忽略某些內(nèi)容為空的值;

2、spider代碼如下:

-- coding: utf-8 --

import scrapyfrom scrapy.http import Requestfrom ueinfo.items import UeinfoItem

class MrueSpider(scrapy.Spider):

name = ’mrue’allowed_domains = [’jd.com’]start_urls = [’http://jd.com/’]def parse(self, response): key='手機(jī)' for i in range(1,2):url='https://search.jd.com/Search?keyword='+str(key)+'&enc=utf-8&page='+str((i*2)-1)#print(url)yield Request(url=url,callback=self.page)def page(self,response): #body=response.body.decode('utf-8','ignore') allid=response.xpath('//p[@class=’p-focus’]//a/@data-sku').extract() for j in range(0,len(allid)):thisid=allid[j]url1='https://item.jd.com/'+str(thisid)+'.html'#print(url1)yield Request(url=url1,callback=self.next)def next(self,response): item=UeinfoItem() item['pinpai']=response.xpath('//ul[@id=’parameter-brand’]/li/@title').extract() #print(item['pinpai']) item['xinghao']=response.xpath('//p[@class=’Ptable’]//p[@class=’Ptable-item’]//dl//dt[text()=’型號(hào)’]/following::*[1]').extract() #print(item['xinghao']) item['nianfen']=response.xpath('//p[@class=’Ptable’]//p[@class=’Ptable-item’]//dl//dt[text()=’上市年份’]/following::*[1]').extract() #print(item['nianfen']) item['yuefen']=response.xpath('//p[@class=’Ptable’]//p[@class=’Ptable-item’]//dl//dt[text()=’上市月份’]/following::*[1]').extract() #print(item['yuefen']) item['caozuoxt']=response.xpath('//p[@class=’Ptable’]//p[@class=’Ptable-item’]//dl//dt[text()=’操作系統(tǒng)’]/following::*[1]').extract() #print(item['caozuoxt']) item['cpupp']=response.xpath('//p[@class=’Ptable’]//p[@class=’Ptable-item’]//dl//dt[text()=’CPU品牌’]/following::*[1]').extract() #print(item['cpupp']) item['cpuhs']=response.xpath('//p[@class=’Ptable’]//p[@class=’Ptable-item’]//dl//dt[text()=’CPU核數(shù)’]/following::*[1]').extract() #print(item['cpuhs']) item['cpuxh']=response.xpath('//p[@class=’Ptable’]//p[@class=’Ptable-item’]//dl//dt[text()=’CPU型號(hào)’]/following::*[1]').extract() #print(item['cpuxh']) item['shuangkalx']=response.xpath('//p[@class=’Ptable’]//p[@class=’Ptable-item’]//dl//dt[text()=’雙卡機(jī)類型’]/following::*[1]').extract() #print(item['shuangkalx']) item['mfnetwangl']=response.xpath('//p[@class=’Ptable’]//p[@class=’Ptable-item’]//dl//dt[text()=’4G網(wǎng)絡(luò)’]/following::*[1]').extract() #print(item['mfnetwangl']) item['fnetwangl']=response.xpath('//p[@class=’Ptable’]//p[@class=’Ptable-item’]//dl//dt[text()=’網(wǎng)絡(luò)頻率(4G)’]/following::*[1]').extract() #print(item['fnetwangl']) item['netwanglplus']=response.xpath('//p[@class=’Ptable’]//p[@class=’Ptable-item’]//dl//dt[text()=’指紋識(shí)別’]/following::*[1]').extract() #print(item['netwanglplus']) item['volte']=response.xpath('//p[@class=’Ptable’]//p[@class=’Ptable-item’]//dl//dt[text()=’高清語(yǔ)音通話(VOLTE)’]/following::*[1]').extract() #print(item['volte']) item['screenstyle']=response.xpath('//p[@class=’Ptable’]//p[@class=’Ptable-item’]//dl//dt[text()=’主屏幕尺寸(英寸)’]/following::*[1]').extract() #print(item['screenstyle']) item['fenbiel']=response.xpath('//p[@class=’Ptable’]//p[@class=’Ptable-item’]//dl//dt[text()=’分辨率’]/following::*[1]').extract() #print(item['fenbiel']) item['dianchirl']=response.xpath('//p[@class=’Ptable’]//p[@class=’Ptable-item’]//dl//dt[text()=’電池容量(mAh)’]/following::*[1]').extract() #print(item['dianchirl']) yield item

pipelines的代碼如下:

-- coding: utf-8 --

import pymysql

class UeinfoPipeline(object):

def __init__(self): self.conn=pymysql.connect(host='127.0.0.1',user='root',passwd='root',db='mysql')def process_item(self, item, spider): try:pinpai=item['pinpai'][0]xinghao=item['xinghao'][0]nianfen=item['nianfen'][0]yuefen=item['yuefen'][0]caozuoxt=item['caozuoxt'][0]coupp=item['cpupp'][0]cpuhs=item['cpuhs'][0]cpuxh=item['cpuxh'][0]shuangkalx=item['shuangkalx'][0]mfnetwangl=item['mfnetwangl'][0]fnetwangl = item['fnetwangl'][0]netwanglplus=item['netwanglplus'][0]volte=item['volte'][0]screenstyle=item['screenstyle'][0]fenbiel=item['fenbiel'][0]dianchirl=item['dianchirl'][0]sql='insert into uems(pinpai,xinghao,nianfen,yuefen,caozuoxt,cpupp,cpuhs,cpuxh,shuangkalx,mwangluo,fwangluo,wangluoplus,volte,screenstyle,fenbian,dianchi)VALUES(’'+pinpai+'’,’'+xinghao+'’,’'+nianfen+'’,’'+yuefen+'’,’'+caozuoxt+'’,’'+coupp+'’,’'+cpuhs+'’,’'+cpuxh+'’,’'+shuangkalx+'’,’'+mfnetwangl+'’,’'+fnetwangl+'’,’'+netwanglplus+'’,’'+volte+'’,’'+screenstyle+'’,’'+fenbiel+'’,’'+dianchirl+'’)'self.conn.query(sql)#print(mfnetwangl)return item except Exception as err:passdef close_spider(self): self.conn.close()

問(wèn)題解答

回答1:

pipelines中的

def close方法定義錯(cuò)誤了

應(yīng)為這樣

def close(self, spider)

至于忽略某些內(nèi)容為空的值用for 可能節(jié)省代碼!

def process_item(self, item, spider): for k,v in item.items():if v == ’’: raise DropItem(repr(item))

標(biāo)簽: Python 編程
主站蜘蛛池模板: 狼人久久尹人香蕉尹人 | 91精品国产91久久久久 | 夜夜操夜夜爽 | 在线观看 国产 | 国产毛片基地 | 日本在线观看免费视频 | 国产高清美女一级a毛片久久 | 免费观看女人一摸全是水 | 欧美精品一区二区三区视频 | 性生话一级国产片 | 青草九九 | 欧美国产三级 | 欧美日一本 | 天天综合色一区二区三区 | 国产日韩精品欧美一区视频 | 国产成人免费网站在线观看 | 黄色三级国产 | 一级做a爱片特黄在线观看免费看 | 国产91专区| 波多野结衣免费免费视频一区 | 美女张开腿给男生桶下面视频 | 日韩欧国产精品一区综合无码 | 国产成人久久久精品一区二区三区 | 久久成| 暖暖在线精品日本中文 | 九九国产精品九九 | 国产视频久久久 | 免费看孕妇毛片全部播放 | 国产日韩欧美精品在线 | 国产午夜精品久久理论片 | 玖玖香蕉视频 | 热99re久久精品香蕉 | 久久一日本道色综合久久m 久久伊人成人网 | 国产午夜精品久久理论片 | 久久精品资源 | 久操福利视频 | 精品香蕉99久久久久网站 | 一区二区三区国产美女在线播放 | 国产男女交性视频播放免费bd | 成人免费在线视频网 | 精品视频在线观看一区二区三区 |