python cookie 獲取某寶商品信息

# //get_goods_from_taobao

import requests

import re

import xlsxwriter

 

cok=''  # 此處寫入登錄之後自己的cookie

# 獲取頁面

def getHTMLText(url):

    headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36'}

    usercookies=cok

    cookies={}

    for a in usercookies.split(';'):

        name,value=a.strip().split('=',1)

        cookies[name]=value

    try:

        r=requests.get(url,cookies=cookies,headers=headers,timeout=60)

        r.raise_for_status()

        r.encoding=r.apparent_encoding

        return r.text

    except:

        return''

#  格式化頁面,查找數據

def parsePage(ilt,html):

    try:

        print('爬取成功')

        plt=re.findall(r'\"view_price\"\:\"[\d\.]*\"',html)

        tlt=re.findall(r'\"raw_title\"\:\".*?\"',html)

        for i in range(len(plt)):

            price=eval(plt[i].split(':')[1])

            title=eval(tlt[i].split(':')[1])

            ilt.append([price,title])

    except:

        print('')

#  打印數據列表

def printGoodList(ilt):

    tplt='{:4}\t{:8}\t{:16}'

    print(tplt.format('序號','價格','名稱'))

    count=0

    for c in ilt:

        count=count+1

        print(tplt.format(count,c[0],c[1]))

#  寫入excel

def writetoexcel(list):

    print('開始創建excel表格')

    book = xlsxwriter.Workbook(u'淘寶數據.xlsx')

    sheet = book.add_worksheet()

    sheet.write(0, 0, '序號')

    sheet.write(0, 1, '名稱')

    sheet.write(0, 2, '價格')

    row = 1

    col = 0

    for index, item in enumerate(list):

        print('寫入第%s行數據'%row)

        sheet.write(row, col, index + 1)  # 寫入序號值

        sheet.write(row, col + 1, item[1])  # 寫入名稱

        sheet.write(row, col + 2, item[0])  # 寫入價格

        row += 1

    print('寫入完成')

    book.close()  # 關閉

 

 

def main():

    goods=input('請輸入想查詢的內容:'.strip())  # 輸入想搜索的商品名稱

    function(){ //隔夜利息 http://www.kaifx.cn/question/kaifx/1752.html

    depth=3  # 爬取的頁數

    start_url='http://s.taobao.com/search?q='+goods  # 搜索接口地址

    infoList=[]

    for i in range(depth):

        try:

            page=i+1

            print('正在爬取第%s頁數據'%page)

            url=start_url+'&s='+str(44*i)

            html=getHTMLText(url)

            parsePage(infoList,html)

        except:

            continue

    printGoodList(infoList)

    writetoexcel(infoList)

 

main()


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章