python每日獲取淘寶首頁關(guān)鍵字推薦詞

每日美圖

介紹

因?yàn)樽约合胫指阋徊ㄌ詫?,?zhǔn)備用于獲取淘寶首頁關(guān)鍵字推薦詞,想化妝品、服裝這種我們是不適合做的競爭太大了。
我們暫時(shí)定為水杯這種小類目的,對于新開的店面除了刷單,最重要的還是自然流量。自然流量都是淘寶買家,搜索關(guān)鍵字顯示商品標(biāo)題,看到商品列表主圖,點(diǎn)擊進(jìn)入查看寶貝詳情,所以想做一個(gè)可以每天定時(shí)將關(guān)于“水杯”關(guān)鍵字的建議詞。這樣的話可以根據(jù)每天的關(guān)鍵字來進(jìn)行修改商品的標(biāo)題。

實(shí)現(xiàn)

我們使用python3.6實(shí)現(xiàn)功能,我們的功能可以分為4個(gè)部分

  1. 獲取關(guān)鍵字建議詞
  2. 將獲取的建議詞寫入csv
  3. 將csv加入郵件附件發(fā)送
  4. 定時(shí)執(zhí)行1-3步
    關(guān)于各部分的代碼如下

獲取關(guān)鍵字建議詞

def getSuggest(key,date,filedirectory):
    try:
        SUGGESTURL = "https://suggest.taobao.com/sug?code=utf-8&q="
        SUGGESTURL = SUGGESTURL + key
        r = requests.get(SUGGESTURL)
        json = r.json()
        return json["result"]
    except:
        l = open(filedirectory + os.path.sep + +date + "log.txt",'a')
        traceback.print_exc(l)
        l.flush()
        l.close()

使用requests模塊直接獲取建議詞,將結(jié)果中json數(shù)據(jù)拿去出來,出錯(cuò)時(shí)將錯(cuò)誤棧內(nèi)容記錄進(jìn)日志中。

將獲取的建議詞寫入csv

def generatecsv(date,filedirectory):
    if not os.path.exists(filedirectory):
        os.mkdir(filedirectory)
    filename = filedirectory + os.path.sep + date + ".csv"
    f = open(filename,'w',encoding='utf-8-sig')
    try:
        items = getSuggest("水杯",date,filedirectory)
        writer = csv.writer(f)
        writer.writerow(["推薦詞","商品代號"])
        for row in items:
            writer.writerow(row)
    except:
        logfilepath = filedirectory + os.path.sep + date + ".log"
        print(logfilepath)
        logfile = open(logfilepath,'a')
        logfile.writelines(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))+os.linesep)
        traceback.print_exc(file=logfile)
        logfile.flush()
        logfile.close()
    finally:
        f.close()

將內(nèi)容以utf-8的編碼寫入csv中,出錯(cuò)時(shí)將錯(cuò)誤棧內(nèi)容記錄進(jìn)日志中。

將csv加入郵件附件發(fā)送

def sendEmail(date,filedirectory):
    filename = filedirectory + os.path.sep + date + ".csv"
    # 第三方 SMTP 服務(wù)
    mail_host = "smtp.163.com"  # SMTP服務(wù)器
    mail_user = "jackwuchenghao@163.com"  # 用戶名
    mail_pass = "********"  # 授權(quán)密碼,非登錄密碼
    sender ="jackwuchenghao@163.com" # 發(fā)件人郵箱(最好寫全, 不然會失敗)
    receivers = ['jackwuchenghao@163.com']  # 接收郵件,可設(shè)置為你的QQ郵箱或者其他郵箱
    content = "this is the new suggest about 水杯"
    message = MIMEText(content, 'plain', 'utf-8')  # 內(nèi)容, 格式, 編碼
    m = MIMEMultipart()
    m.attach(message)
    csvfile = MIMEApplication(open(filename, 'rb').read())
    csvfile.add_header('Content-Disposition', 'attachment', filename=date + ".csv")
    m.attach(csvfile)
    m['Subject'] = "水杯"
    m['From'] = "{}".format(sender)
    m['To'] = ",".join(receivers)
    smtpserver = smtplib.SMTP_SSL(mail_host, 465)  # 啟用SSL發(fā)信, 端口一般是465
    try:
        smtpserver.login(mail_user, mail_pass)  # 登錄驗(yàn)證
        smtpserver.sendmail(sender, receivers, m.as_string())  # 發(fā)送
    except:
        logfilepath = filedirectory + os.path.sep + date + ".log"
        print(logfilepath)
        logfile = open(logfilepath,'a')
        logfile.writelines(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))+os.linesep)
        traceback.print_exc(file=logfile)
        logfile.flush()
        logfile.close()
    finally:
        smtpserver.quit()

發(fā)送郵件這部分代碼有點(diǎn)多就不全部貼出來了,有興趣的讀者可以點(diǎn)擊文末的鏈接下載查看

定時(shí)執(zhí)行1-3步

if __name__ == "__main__":
    schedule.every(1).day.at("00:00").do(dailywork)
    while True:
        schedule.run_pending()

使用schedule模塊實(shí)現(xiàn)定時(shí)執(zhí)行函數(shù)的功能。

python與模塊安裝難點(diǎn)

其實(shí)代碼的實(shí)現(xiàn)不是特別難的事情,但是在服務(wù)器的運(yùn)行還是比較麻煩的,在centos上的步驟如下,因?yàn)橄到y(tǒng)自帶ython2.7千萬別刪除,因?yàn)閥um是依賴于python2.7的:

yum -y install zlib*
yum install openssl-devel zilb-devel python3-devel
wget https://www.python.org/ftp/python/3.6.1/Python-3.6.1.tgz -P /opt
tar -zxvf Python-3.6.1.tgz
cd Python-3.6.1
./configure --with-ssl --prefix=/usr/local/python3
make && make install
ln -s /usr/local/python3/bin/python3 /usr/bin/python3
ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3
vim ~/.bash_profile
//添加`PATH=$PATH:$HOME/bin:/usr/local/python3/bin`保存退出
source ~/.bash_profile
wget --no-check-certificate  https://pypi.python.org/packages/source/p/pip/pip-8.0.2.tar.gz#md5=3a73c4188f8dbad6a1e6f6d44d117eeb
tar -zxvf pip-8.0.2.tar.gz
cd pip-8.0.2
python3 setup.py build
python3 setup.py install
pip3 install requests
pip3 install schedule

總結(jié)

  1. 對于想做淘寶的小商戶來說最好一開始不要做競爭激烈的類目。
  2. 自然流量的關(guān)鍵點(diǎn)為標(biāo)題、主圖、寶貝詳情。
  3. 本文的代碼可以點(diǎn)擊鏈接查看。
  4. 在centos上安裝python3.6時(shí)注意,因?yàn)閏entos系統(tǒng)自帶python2.7千萬別刪除,因?yàn)閥um是依賴于python2.7的。
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容