python作品-学了一个多月python了,终于有能拿得出手的作品了

# -*- coding: utf-8 -*-

import scrapy

import json

from Taobao.items import TaobaoItem

# url编码

from urllib.parse import quote

# url解码

from urllib.parse import unquote

class TaobaoSpider(scrapy.Spider):

name = 'taobao'

# allowed_domains = ['taobao.com/']

page = input('请输入打印页数:')

Quote = input('请输入要搜索的商品名')

start_urls = ['https://ai.taobao.com/search/getItem.htm?_tb_token_=e3d450b1e33e&__ajax__=1&pid=mm_33793785_3431230_471812702&unid=&clk1=&page={}&pageSize=60&pvid=200_11.224.194.119_358_1541678031255&squareFlag=&sourceId=search&ppathName=&supportCod=&city=&ppath=&dc12=&pageNav=false&itemAssurance=&fcatName=&price=&cat=&from=&tmall=&key={}&fcat=&ppage=0&debug=false&maxPageSize=200&sort=&exchange7=&custAssurance=&postFree=&npx=50&location='.format(int(page),quote(Quote,'utf-8'))]

a = 1

def parse(self, response):

js = json.loads(response.body)['result']['auction']

f = open('{}.csv'.format(self.Quote), 'w', encoding='utf-8')

f.write("商品名,价格,店名 ")

for text in js:

dict = {

'name' : text['description'],

'nick' : text['nick'],

'realPrice' : text['realPrice'],

}

f.write("{name},{realPrice},{nick} ".format(**dict))

origPicUrl = 'https:' + text['origPicUrl']

item = TaobaoItem()

item['origPicUrl'] = origPicUrl

yield item

f.close()

if self.page != '1':

print('=' * 40 + '第' + self.page + '页下载完毕' + '=' * 40)

if self.page != '1':

for n in range(2,int(self.page)):

yield scrapy.Request('https://ai.taobao.com/search/getItem.htm?_tb_token_=e3d450b1e33e&__ajax__=1&pid=mm_33793785_3431230_471812702&unid=&clk1=&page={}&pageSize=60&pvid=200_11.224.194.119_358_1541678031255&squareFlag=&sourceId=search&ppathName=&supportCod=&city=&ppath=&dc12=&pageNav=false&itemAssurance=&fcatName=&price=&cat=&from=&tmall=&key=python%E7%BC%96%E7%A8%8B%E4%BB%8E%E5%85%A5%E9%97%A8%E5%88%B0%E5%AE%9E%E6%88%98&fcat=&ppage=0&debug=false&maxPageSize=200&sort=&exchange7=&custAssurance=&postFree=&npx=50&location='.format(n),self.parse)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值