python爬取内容为空怎么剔除_用scrapy爬取网站内容时,如何忽略某些内容为空的值;...
我爬取京东上所有手机信息时会碰到如下问题:
1、返回值过多,如下图片所示:
2、spider代码如下:
-- coding: utf-8 --
import scrapy
from scrapy.http import Request
from ueinfo.items import UeinfoItem
class MrueSpider(scrapy.Spider):
name = 'mrue'
allowed_domains = ['jd.com']
start_urls = ['http://jd.com/']
def parse(self, response):
key="手机"
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("//div[@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("//div[@class='Ptable']//div[@class='Ptable-item']//dl//dt[text()='型号']/following::*[1]").extract()
#print(item["xinghao"])
item["nianfen"]=response.xpath("//div[@class='Ptable']//div[@class='Ptable-item']//dl//dt[text()='上市年份']/following::*[1]").extract()
#print(item["nianfen"])
item["yuefen"]=response.xpath("//div[@class='Ptable']//div[@class='Ptable-item']//dl//dt[text()='上市月份']/following::*[1]").extract()
#print(item["yuefen"])
item["caozuoxt"]=response.xpath("//div[@class='Ptable']//div[@class='Ptable-item']//dl//dt[text()='操作系统']/following::*[1]").extract()
#print(item["caozuoxt"])
item["cpupp"]=response.xpath("//div[@class='Ptable']//div[@class='Ptable-item']//dl//dt[text()='CPU品牌']/following::*[1]").extract()
#print(item["cpupp"])
item["cpuhs"]=response.xpath("//div[@class='Ptable']//div[@class='Ptable-item']//dl//dt[text()='CPU核数']/following::*[1]").extract()
#print(item["cpuhs"])
item["cpuxh"]=response.xpath("//div[@class='Ptable']//div[@class='Ptable-item']//dl//dt[text()='CPU型号']/following::*[1]").extract()
#print(item["cpuxh"])
item["shuangkalx"]=response.xpath("//div[@class='Ptable']//div[@class='Ptable-item']//dl//dt[text()='双卡机类型']/following::*[1]").extract()
#print(item["shuangkalx"])
item["mfnetwangl"]=response.xpath("//div[@class='Ptable']//div[@class='Ptable-item']//dl//dt[text()='4G网络']/following::*[1]").extract()
#print(item["mfnetwangl"])
item["fnetwangl"]=response.xpath("//div[@class='Ptable']//div[@class='Ptable-item']//dl//dt[text()='网络频率(4G)']/following::*[1]").extract()
#print(item["fnetwangl"])
item["netwanglplus"]=response.xpath("//div[@class='Ptable']//div[@class='Ptable-item']//dl//dt[text()='指纹识别']/following::*[1]").extract()
#print(item["netwanglplus"])
item["volte"]=response.xpath("//div[@class='Ptable']//div[@class='Ptable-item']//dl//dt[text()='高清语音通话(VOLTE)']/following::*[1]").extract()
#print(item["volte"])
item["screenstyle"]=response.xpath("//div[@class='Ptable']//div[@class='Ptable-item']//dl//dt[text()='主屏幕尺寸(英寸)']/following::*[1]").extract()
#print(item["screenstyle"])
item["fenbiel"]=response.xpath("//div[@class='Ptable']//div[@class='Ptable-item']//dl//dt[text()='分辨率']/following::*[1]").extract()
#print(item["fenbiel"])
item["dianchirl"]=response.xpath("//div[@class='Ptable']//div[@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:
pass
def close_spider(self):
self.conn.close()
python爬取内容为空怎么剔除_用scrapy爬取网站内容时,如何忽略某些内容为空的值;...相关推荐
- python爬虫scrapy爬取新闻标题及链接_python爬虫框架scrapy爬取梅花网资讯信息
原标题:python爬虫框架scrapy爬取梅花网资讯信息 一.介绍 本例子用scrapy-splash爬取梅花网(http://www.meihua.info/a/list/today)的资讯信息, ...
- python爬取有道翻译的代码_使用python2爬取有道翻译
爬虫的核心思想:模拟浏览器正常访问服务器,一般情况只要浏览器能访问的,都可以爬,如果被反爬,则考虑反复测试添加Request Header数据,知道可以爬取为止. 反爬思路目前知道的有:User-Ag ...
- python爬取京东手机数据_用scrapy爬取京东的数据
本文目的是使用scrapy爬取京东上所有的手机数据,并将数据保存到MongoDB中. 一.项目介绍 主要目标 1.使用scrapy爬取京东上所有的手机数据 2.将爬取的数据存储到MongoDB 环境 ...
- scrapy mysql 豆瓣_利用Scrapy爬取豆瓣图书并保存至Mysql数据库
Scrapy是一个纯Python语言写的爬虫框架,本次用它来爬取豆瓣图书的数据. 准备工作 没有安装Scrapy的同学需要安装一下,有两种方式安装: 安装了Anaconda的同学直接在命令行输入con ...
- python 爬虫源码 selenium并存储数据库_使用pythonSelenium爬取内容并存储MySQL数据库的实例图解...
这篇文章主要介绍了python Selenium爬取内容并存储至MySQL数据库的实现代码,需要的朋友可以参考下 前面我通过一篇文章讲述了如何爬取CSDN的博客摘要等信息.通常,在使用Selenium ...
- python爬取b站评论_Python爬虫框架:scrapy抓取B站博人传评论数据
1. B站博人传评论数据爬取简介 今天想了半天不知道抓啥,去B站看跳舞的小姐姐,忽然看到了评论,那就抓取一下B站的评论数据,视频动画那么多,也不知道抓取哪个,选了一个博人传跟火影相关的,抓取看看.网址 ...
- python爬网页、爬到前几个就不动了_python scrapy 爬取起点小说,爬虫停止在第四页不动了...
如题,我在测试爬虫的时候,终端出现了如下代码: 2019-04-20 15:04:51 [scrapy.core.engine] DEBUG: Crawled (200) (referer: http ...
- scrapy mysql 词云_利用Scrapy爬取姓名大全作词云分析
scrapy介绍 Scrapy 是一套基于Twisted.纯python实现的异步爬虫框架,用户只需要定制开发几个模块就可以轻松的实现一个爬虫,用来抓取网页内容以及各种图片,相当的方便- 整体架构和组 ...
- 基于selenium+scrapy爬取复仇者联盟4豆瓣影评数据
基于selenium+scrapy爬取复仇者联盟4豆瓣影评数据 参考资料: 黑马程序员爬虫教程 静觅爬虫教程 mac下anaconda安装selenium+PhantomJS scrapy下载中间件结 ...
最新文章
- 如何将PDF转换成JPEG图片?
- php mysql事务
- 请回答! 2018泛娱乐风口
- mina mysql_Mina学习笔记(二)
- 【渝粤题库】陕西师范大学202891 基于web的程序设计
- Oracle数据库imp
- MyEclipse10.6 安装SVN插件方法及插件下载地址
- Kafka 设计与原理详解(一)
- 大数据 客户标签体系_CIO创享任寅姿:面向业务的数据资产建设方法论——标签类目体系...
- error:there‘s no Qt version assigned to project please assign a Qt installation in qt project settin
- 判断是否打开相机权限,如果没有打开相机权限
- 我的世界神奇宝贝服务器怎么修改6v,我的世界神奇宝贝mod修改精灵6V满努力等级图文教程...
- 计算机主机硬盘的拆装,机械硬盘怎么拆开?机械硬盘拆卸方法
- 梦三国测试服显示连接服务器失败,我的登陆进去以后说与服务器失去连接怎么回事...
- 给定一个整型变量a,写两段代码,第一个设置a的bit 3,第二个清除a 的bit 3。在以上两个操作中,要保持其它位不变。
- 面阵相机该如何选型?
- CopyOnWriteArraySet
- pycharm调试模式
- Sylvester不等式证明
- 计算机网络国际会议排名,科学网—计算机相关国际会议排名 - 刘耀的博文