本帖最后由 叫我小王叔叔 于 2021-5-10 10:35 编辑
自学python,一直观察并抄袭大佬们代码,整合了一下,有了下面这个东西,参考了@白白io的api和@culprit的云函数。
[Python] 纯文本查看 复制代码 import requests,random,json
from bs4 import BeautifulSoup
def UserAgent(): #随机获取请求头
user_agent_list = ['Mozilla/5.0 (Windows NT 6.2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1464.0 Safari/537.36',
'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.16 Safari/537.36',
'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.3319.102 Safari/537.36',
'Mozilla/5.0 (X11; CrOS i686 3912.101.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36',
'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.93 Safari/537.36',
'Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1667.0 Safari/537.36',
'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:17.0) Gecko/20100101 Firefox/17.0.6',
'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1468.0 Safari/537.36',
'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2224.3 Safari/537.36',
'Mozilla/5.0 (X11; CrOS i686 3912.101.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36']
UserAgent=random.choice(user_agent_list)
return UserAgent
def wxpush(text,article_url):
headers = {"User-Agent": UserAgent(),"Content-Type":"application/json"}
url = "http://wxpusher.zjiecode.com/api/send/message"
data = {
"appToken": "你自己的",
"content": text,
#"summary":text,
"contentType":2,
"uids": ["你自己的"],
"url": article_url
}
r = requests.post(url, headers=headers, data=json.dumps(data))
def check(keys):
for key in keys:
url = "https://api.smzdm.com/v1/list?f=wxapp&wxapp=zdmapp&order=time&keyword=" + key + "&offset=0&limit=3&category_name="
s = requests.session()
r = s.get(url, headers={"User-Agent": UserAgent()})
#r.encoding='gb2312'
#print(r.text)
data=r.json()
goods=data["data"]["rows"]
#print(goods)
for good in goods:
article_title = good["article_title"]
article_price = good["article_price"]
#article_format_date = good["article_format_date"]
#article_pic = good["article_pic"]
#article_mall = good["article_mall"]
article_url = good["article_url"]
text=article_title+article_price
wxpush(text,article_url)
def checkuids(uids):
goods_list = []
try:
for uid in uids:
url = "https://zhiyou.smzdm.com/member/" + uid + "/baoliao/"
response = requests.get(url, headers={"User-Agent": UserAgent()}).content
soup = BeautifulSoup(response, 'html.parser', from_encoding='utf-8')
html = soup.find_all(name='div', attrs={'class': 'pandect-content-title'})
for i in html:
goods_list.append(i.a.get_text().strip() + ' ——> ' + i.a['href'].strip())
wxpush(i.a.get_text().strip(), i.a['href'].strip())
if goods_list[0]:
break;
except Exception as e:
print(e)
if __name__ == "__main__":
goods=['除湿机']#商品关键字,可以自己添加
check(goods)
uids=['9687682701', '5160345630']#什么值得买爆料人ID,可以自己添加
checkuids(uids)
应该再加一个本地的文件,保存上次更新的东西,如果有更新就推送,没有就歇着。
初学,没什么方法,能实现需求就好。 |