smzdm 爬虫

张大妈是个好网站,不但实用还很好爬,感恩

import requests
import json
import time
import csv

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.87 Safari/537.36'}
wxnotes = 'server酱API'
f = open('smzdm.csv', 'wt', newline='', encoding='UTF-8')  #创建csv文件
writer = csv.writer(f) 
writer.writerow(('zhi','title','id','price','link','date'))


def get_info(url, keyword):
    res = requests.get(url, headers=headers)
    resjson = json.loads(res.text)
    reslist = resjson['data']
    for i in range(0, 20):
        if 'article_worthy' in reslist[i]:
                worthy = int(reslist[i]['article_worthy'])
                unworthy = int(reslist[i]['article_unworthy'])
                collection = int(reslist[i]['article_collection'])
                title = reslist[i]['article_title']
                zhi = worthy - unworthy
                artid = reslist[i]['article_id']  # smzdm页面ID
                price = reslist[i]['article_price']
                link = reslist[i]['article_link']
                date =  reslist[i]['article_date']
                if worthy - unworthy >= 50 or collection >= 50:
                    writer.writerow((zhi,title,artid,price,link,date))  # 写入文件
                    news = {
                        'zhi': zhi,
                        'title':title,
                        'id':artid,
                        'price':price,
                        'link':link,
                        'date':date
                     }
                    print(news)  # 给自己看的
                    if keyword != '':
                        if (title.find(keyword)) != -1:
                            text = "{zhi}|名称:{title}" \
                                .format(title=title,
                                        zhi=zhi)
                            desp = "详情:https://www.smzdm.com/p/{link}\n价格:{price}\n购买地址:{gobuy}" \
                                .format(link=artid,
                                        price=price,
                                        gobuy=link)

                            params = {
                                "text": text,
                                "desp": desp
                            }
                            notes = requests.post(wxnotes, params=params)  # server酱推送到微信


if __name__ == '__main__':
    papges = int(input('爬多少页: '))
    keyword = input(('关键字(不需要的话直接回车):'))
    for i in range(1, papges, 1):
        num = i * 20
        urls = "https://www.smzdm.com/homepage/json_more?p={p}&past_num={num}"\
            .format(p=i, num=num)
        get_info(urls, keyword)
        time.sleep(5)


因为不太熟python语法所以写的很烂,你们不要管我!!!
秒杀的爬虫也做好了但是有点沙雕所以不太想发,我还是一个有尊严的人!