爬取的图片地址用get()可以获取,用getall()则为空,由于提取地址后还要保存,get()得到的结果进行本地存储就又会报错,这是什么原因呢,应该怎么处理
get()获取结果:
get()获取图片地址下载报错:
get_all是返回选中的img中的src属性数组,并不等于none,所以下面tupians==None
的判断就失败了。而且 上个问题已说了,第一屏的img才有src属性,其他是lz_src存储图片路径,只能用get一个一个获取,当得遍历到没有src的属性的img时后去lz_src属性值
字符串内容
,不是数组,不用for...来遍历
而且获取游戏标题的代码也错了,整体代码如下
import scrapy
class Game4399Spier(scrapy.Spider):
name = 'game4399'
allowed_domains = ['www.4399.com']
start_urls = ['https://www.4399.com/flash/gamehw.htm']
def parse(self, response):
zxings=response.xpath('//div[@class="w_box cf"][1]/ul/li')
for zxing in zxings:
tupians=zxing.xpath('a/img/@src').get()
if tupians==None:#src没有值获取lz_src属性值
tupians=zxing.xpath('a/img/@lz_src').get()
tupians='https'+tupians
gnames=zxing.xpath('a/b/text()').get()#获取对应标题
print(gnames,tupians)
item=G4399Item(gnames=gnames,img_tupians=tupians)
yield item
为什么还要用getall()?直接去掉试试
tupians=zxing.xpath('a/img/@1z_src')