愛情公寓自動留言自動搜尋程式[Python] v1.0.3

最後列出來的名單排序好像有點問

找了一下發現是之前為了消除list裡重複的id

用了set,結果排序整個亂掉

修改好了應該不會在排序問題

傳送順序是依上線時間

自動搜尋留言程式:按我下載
詳細介紹:愛情公寓自動留言 自動搜尋 程式[Python]
備註:如果執行時出現cannot get zipimporter instance那就是要把檔案放在沒中文路徑下才能執行

以下是原始碼:

# -*- coding: cp950 -*-
'''
Created on 2011/12/13

@author: KK
'''
import tkinter as tk
import tkinter.messagebox
import http.cookiejar
import urllib.request
import urllib.parse
import re
import threading
import random

#設定標頭檔和cookie
cookie_jar = http.cookiejar.CookieJar()
cjhdr = urllib.request.HTTPCookieProcessor(cookie_jar)
opener = urllib.request.build_opener(cjhdr)

#------------------------------
def logins():
 cookie_jar.clear()#丟棄所有session和cookies
 user = user_var.get()
 password = password_var.get()
 url="https://login.i-part.com.tw/login.php"
 values={
 "f":"/file/file_my.php?",
 "password":password,
 "r":"http://www.i-part.com.tw/index.php",
 "username":user
 }

 datas=urllib.parse.urlencode(values)#轉為url資料傳輸碼
 binary_data = datas.encode('cp950')
 opener.open(url,binary_data)#登入
 #登入後進入首頁抓取姓名
 user_cookie = cheack_cookie()
 if user_cookie:
 msg_var.set('登入成功')
 else:
 msg_var.set('登入失敗')
#------------------------------
def cheack_cookie():
 cookies = dict([(cookie.name, cookie.value) for cookie in cookie_jar])
 if 'SID' in cookies.keys():
 return cookies['SID']
 else:
 return False

#------------------------------
def seach():
 msg_posting.config(text=str('正在讀取中...'))
 seach_val={
 "Submit2":"立即尋找",
 "s_age[0]":age1_var.get(),
 "s_age[1]":age2_var.get(),
 "s_country":"1",
 "s_couple":"0",
 "s_from":citys[citymenu.get()],
 "s_height[]":"",
 "s_height[]":"",
 "s_photo":"1",
 "s_sex":sex_var.get(),
 "s_weight[]":"",
 "s_weight[]":"",
 "s_zone":"",
 "type":"simple"
 }
 seach_url = urllib.parse.urlencode(seach_val)
 seach_binary = seach_url.encode('cp950')
 seach_data = opener.open("http://www.i-part.com.tw/search/query_fast.php",seach_binary).read().decode('cp950','replace')
 id_data = re.findall("""viewfile.php?u=([0-9][0-9][0-9]*?)"""", seach_data)

 pCount = re.search("""name="pCount" value="([0-9]*?)"""", seach_data).group(1)
 #幾筆資料
 rows = re.search("""name="rows" value="([0-9]*?)"""", seach_data).group(1)

 #總共有幾頁
 page = int(rows)//18
 #把頁數設定給msgpage_var
 msgpage_var.set("總頁數:"+str(page))

 for page in range(1,page):
 seachs_val={
 "pCount":pCount,
 "page": page,
 "rows":rows,
 "user_age[0]":age1_var.get(),
 "user_age[1]":age2_var.get(),
 "user_couple":"0",
 "user_from":citys[citymenu.get()],
 "user_height[]":"",
 "user_height[]":"",
 "user_photo":"1",
 "user_sex":sex_var.get(),
 "user_weight[]":"",
 "user_weight[]":"",
 "user_zone":""
 }
 seachs_url = urllib.parse.urlencode(seachs_val)
 seachs_binary = seachs_url.encode('cp950')
 seachs_data = opener.open("http://www.i-part.com.tw/search/query_advanced.php",seachs_binary).read().decode('cp950','replace')
 id_datas = re.findall("""viewfile.php?u=([0-9][0-9][0-9]*?)"""", seachs_data)
 id_data.extend(id_datas)
 id_list = sorted(set(id_data),key=id_data.index)
 people_list = read_depots("people.txt")
 postmsg(id_list,people_list)

#--------------------------------------------------
def postmsg(id_list=[],people_list=[]):
 if len(id_list) > 0:
 if id_list[0] in people_list:
 msg_posting.config(text=str('重複名單' + id_list.pop(0)))
 msg_posting.after(200, lambda:postmsg(id_list,people_list))
 else:
 people_id = id_list.pop(0)
 msg_posting.config(text=str('正在傳送訊息給' + people_id))
 postmsgs = post.get("1.0",tk.END)
 postmsgs = postmsgs.encode('cp950')
 url="http://www.i-part.com.tw/post_office/stamp_addmsg.php?"
 values={
 "Pintro":"",
 "Submit":"Submit",
 "msg_ValidateKey":"",
 "msg_mood":"1",
 "msg_private":"1",
 "msg_text_post":postmsgs,
 "msg_to":people_id,
 "pasteStamp":"",
 "sm":"",
 }

 datas=urllib.parse.urlencode(values)#轉為url資料傳輸碼
 binary_data = datas.encode('cp950')
 response = opener.open(url,binary_data).read().decode('cp950','replace')#登入
 if '留言成功' in response:
 peoplefile = open("people.txt", "a")
 peoplefile.write(people_id+"n")
 peoplefile.close()
 else:
 peoplefile = open("error.txt", "a")
 peoplefile.write(people_id+"n")
 peoplefile.close()

 sec = random.randint(30000,40000)#隨機秒數
 msg_posting.after(sec, lambda:postmsg(id_list,people_list))
 else:
 msg_posting.config(text=str('傳送完成'))
#--------------------------------------------------
def thread_seach():
 user_cookie = cheack_cookie()
 if user_cookie:
 thread = threading.Thread(target=seach)
 thread.start()
 else:
 tkinter.messagebox.showinfo("訊息", "請先登入" )
def thread_logins():
 msg_var.set('登入中...')
 thread = threading.Thread(target=logins)
 thread.start()

#----------------------------------------
def read_depots(file):
 depots = []
 depots_f = open(file)
 for line in depots_f:
 depots.append(line.rstrip())
 return depots

#----------------------------------------

app = tk.Tk()
app.title("愛情公寓自動留言")
app.geometry('496x350+200+100')

global_font=("Arial",11)

user_var = tk.StringVar()
user_var.set("")
password_var = tk.StringVar()
password_var.set("")

msg_var = tk.StringVar()
msg_var.set("")

#年齡變數
age1_var = tk.IntVar()
age1_var.set(18)
age2_var = tk.IntVar()

#城市變數(預設台中)
citymenu = tk.StringVar()
citymenu.set("台中市")

#性別變數
sex_var = tk.StringVar()
sex_var.set("M")

#下方訊息
msgposting_var = tk.StringVar()
msgposting_var.set("")

#搜尋資料的頁數-變數
msgpage_var = tk.StringVar()
msgpage_var.set("總頁數:")

#----------------------------------------------
#帳號欄位
user_lab = tk.Label(app, font=global_font, text="帳號:").place( height=30,x=10,y=10)
user=tk.Entry( app, font=global_font, width=10, textvariable = user_var).place( height=20,x=50,y=15)

#密碼欄位
password_lab = tk.Label(app, font=global_font, text="密碼:").place( height=30,x=130,y=10)
password=tk.Entry( app, font=global_font, width=10, show='*', textvariable = password_var).place( height=20,x=170,y=15)

#登入按鈕
login = tk.Button(app, font=global_font, text = "登入", width = 10, command = thread_logins).place( height=30, width=60,x=270,y=10)

#顯示訊息
msg = tk.Label(app, font=global_font, textvariable = msg_var).place( height=30,x=340,y=10)

#--------------------
#年齡1
tk.Label( app, font=global_font, text="年齡:").place( height=30,x=10,y=50)
tk.Entry( app, font=global_font, width=2, textvariable = age1_var).place( height=20,x=50,y=55)

#年齡2
tk.Label(app, font=global_font, text="~").place( height=30,x=70,y=50)
tk.Entry( app, font=global_font, width=2, textvariable = age2_var).place( height=20,x=85,y=55)

#city_menu
city_id = read_depots("city_id.txt")
city = read_depots("city.txt")
citys = dict(zip(city,city_id))
tk.OptionMenu(app, citymenu, *city).place( height=30,x=110,y=50)

sex1 = tk.Radiobutton(app, text="男", variable=sex_var, value="M").place( height=30,x=200,y=50)
sex2 = tk.Radiobutton(app, text="女", variable=sex_var, value="F").place( height=30,x=240,y=50)
#-----------------------------------------
#text訊息框
post = tk.Text(app, font=global_font,height=10,width=59)
post.place( height=200,x=10,y=100)
#-----------------------------------------
#下方顯示訊息
msg_posting = tk.Label(app, font=global_font)
msg_posting.place(height=30,x=10,y=310)
#下方顯示訊息
msg_page = tk.Label(app, font=global_font, textvariable = msgpage_var)
msg_page.place(height=30,x=200,y=310)

#送出按紐
submit = tk.Button(app, font=global_font, text = "送出", command = thread_seach).place( height=30,x=300,y=310)

#------------------------------------------
app.mainloop()

發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *