技术:python爬虫 和 PYQT5
因为自己贴吧关注了一千多个,所以就自己动手做了一个贴吧的签到工具,用python爬虫,然后想做可视化就也用了QT
具体方法就是: 遍历获取到关注的贴吧,然后模拟手机客户端签到
在B站上传了软件教程视频
https://www.bilibili.com/video/av89030084
https://www.bilibili.com/video/av89124795
程序如图:
贴吧爬虫代码:
[Python] 纯文本查看 复制代码 import requests
from lxml import etree
import re
import time
def extract_cookies(cookie):
#这个函数参考网络
[i]"""[/i][i]从浏览器或者[/i][i]request headers[/i][i]中拿到[/i][i]cookie[/i][i]字符串,提取为字典格式的[/i][i]cookies"""
[/i][i] [/i]cookies = dict([l.split("=", 1) for l in cookie.split("; ")])
return cookies
class tieba_sign(object):
def __init__(self,cookie):
self.iphon_headers = { "User-Agent": 'Mozilla/5.0 (compatible; MSIE 10.0; Windows Phone 8.0; Trident/6.0; IEMobile/10.0; ARM; Touch; NOKIA; Lumia 920)'}
self.cookie = extract_cookies(cookie)
cookiejar = requests.utils.cookiejar_from_dict(self.cookie)
self.session = requests.session()
self.session.cookies = cookiejar
self.session.headers = self.iphon_headers
self.tieba_url_list = []
self.url = "http://tieba.baidu.com/f/like/mylike?&pn=1"
def get_end_page(self):
res = self.session.get(self.url)
res_html = etree.HTML(res.content)
str_end_page = res_html.xpath('/ html / body / div[1] / div[2] / div / a[last()]/@href')[0]
numb_page = re.findall("\d+",str_end_page)[0]
self.end_page = int(numb_page)
def get_tieba_url(self):
self.get_end_page()
tieba_url = "http://tieba.baidu.com/f/like/mylike?&pn="
for i in range(50,self.end_page+1):
url = tieba_url + str(i)
url_data = self.session.get(url).content.decode('gbk')
url_list= re.findall(r'href="(/f\?kw=.*?)" title=', url_data)
self.tieba_url_list.append(url_list)
def get_sign_url(self):
starttime = time.time()
self.get_tieba_url()
for urls in self.tieba_url_list:
for url in urls:
url = 'https://tieba.baidu.com'+url
# print(url)
tieba_sign = self.session.get(url)
tieba_sign_html = etree.HTML(tieba_sign.content)
tieba_name = tieba_sign_html.xpath('/html/body/div/div[1]/text()')
sign_url = tieba_sign_html.xpath('/html/body/div/div[1]/table[2]/tr/td[2]/a/@href')
if sign_url == []:
print(tieba_name,tieba_sign_html.xpath('/html/body/div/div[1]/table[2]/tr/td[2]/span/text()'))
else:
url = "https://tieba.baidu.com"+sign_url[0]
# print("签到连接",url)
self.session.get(url)
print(tieba_name,"签到成功")
endtime = time.time()
print("程序所执行时间:",endtime - starttime,"秒")
if __name__ == "__main__":
cookie = ""
a = tieba_sign(cookie)
a.get_sign_url()
可视化GUI代码:
[Python] 纯文本查看 复制代码 # -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'untitled.ui'
#
# Created by: PyQt5 UI code generator 5.14.0
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
import sys
import tieba_Sign_in
class Ui_Time(object):
def setupUi(self, Time):
Time.setObjectName("Time")
Time.resize(500, 300)
Time.setMinimumSize(QtCore.QSize(500, 300))
Time.setMaximumSize(QtCore.QSize(500, 300))
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap(":/newPrefix/icon.jpg"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
Time.setWindowIcon(icon)
self.inputcookie = QtWidgets.QLineEdit(Time)
self.inputcookie.setGeometry(QtCore.QRect(140, 70, 331, 31))
self.inputcookie.setObjectName("inputcookie")
self.cookies = QtWidgets.QLabel(Time)
self.cookies.setGeometry(QtCore.QRect(40, 70, 81, 21))
self.cookies.setObjectName("cookies")
self.inputpage = QtWidgets.QLineEdit(Time)
self.inputpage.setGeometry(QtCore.QRect(130, 30, 140, 40))
self.inputpage.setObjectName("inputpage")
self.inputpage.resize(30,30)
self.label = QtWidgets.QLabel(Time)
self.label.setGeometry(QtCore.QRect(170,25,100,40))
self.label.setText("------>>>>>")
self.inputpage2 = QtWidgets.QLineEdit(Time)
self.inputpage2.setGeometry(QtCore.QRect(250,30,160,40))
self.inputpage2.setObjectName("inputpage")
self.inputpage2.resize(30,30)
self.page = QtWidgets.QLabel(Time)
self.page.setGeometry(QtCore.QRect(40, 30, 81, 21))
self.page.setObjectName("page")
self.signbutton = QtWidgets.QPushButton(Time)
self.signbutton.setGeometry(QtCore.QRect(320, 130, 131, 51))
self.signbutton.setObjectName("signbutton")
self.label_3 = QtWidgets.QLabel(Time)
self.label_3.setGeometry(QtCore.QRect(20, 200, 461, 91))
self.label_3.setStyleSheet("font: 40pt \"Adobe Arabic\";\n"
"\n"
"background-color: rgb(170, 255, 255);")
self.label_3.setObjectName("label_3")
self.plainTextEdit = QtWidgets.QTextEdit(Time)
self.plainTextEdit.setGeometry(QtCore.QRect(20, 110, 251, 81))
self.plainTextEdit.setObjectName("plainTextEdit")
self.plainTextEdit.setText(" 1. 请输入正确的cookie\n"
" 2. cookie获取请自己百度\n"
" 3. 百度贴吧的cookie\n"
" 4. 签到较慢,可挂在后台签到\n")
self.retranslateUi(Time)
QtCore.QMetaObject.connectSlotsByName(Time)
def retranslateUi(self, Time):
_translate = QtCore.QCoreApplication.translate
Time.setWindowTitle(_translate("Time", "贴吧签到工具 By:Time"))
self.cookies.setText(_translate("Time", "贴吧Cookies:"))
self.inputpage.setText(_translate("Time", "1"))
self.inputpage2.setText(_translate("Time", "10"))
self.page.setText(_translate("Time", "贴吧页数:"))
self.signbutton.setText(_translate("Time", "签到"))
self.label_3.setText(_translate("Time", "By:Time 仅供学习"))
self.signbutton.pressed.connect(self.button_sign)
def button_sign(self):
try:
# self.inputcookie.setText("BIDUPSID=B8EDA9FE991BD4651BADBEE9F5CE7384; PSTM=1578200750; BAIDUID=B8EDA9FE991BD465276CD5C0FA20DE70:FG=1; TIEBA_USERTYPE=847a0c6090168851c0f40498; bdshare_firstime=1578240160535; TIEBAUID=c57bab2c7b74a61303f2cb42; BDUSS=1hzTW5zT05rNGUwNmxTaEFiN1lwdTdoQ2V1cTMwVFhIMGZaR0RRSmowMUFaVk5lRVFBQUFBJCQAAAAAAAAAAAEAAABTfyEt2K9XZW6~4QAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEDYK15A2CteR; STOKEN=a370f5f57f95492d9174d3796b599d3dfd3b284d15190dc62842168fbb706a60; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; Hm_lvt_98b9d8c2fd6608d564bf2ac2ae642948=1580369330,1580369790,1580372921,1580436563; delPer=0; PSINO=7; H_PS_PSSID=1456_21084; PMS_JT=%28%7B%22s%22%3A1580495261961%2C%22r%22%3A%22http%3A//tieba.baidu.com/home/main%3Fun%3D%25E4%25B8%25BFWen%25E9%2585%25B7%26id%3Dtb.1.ac720bbb.7IhyDaUawadBH81SgSAzZg%3Ft%3D1579471960%26fr%3Dindex%26red_tag%3Dc1659334063%22%7D%29; Hm_lpvt_98b9d8c2fd6608d564bf2ac2ae642948=1580495263")
stare_page = int(self.inputpage.text())
page = int(self.inputpage2.text())+1
cookie = self.inputcookie.text()
# print(cookie)
tieba_Sign_in.stear(stare_page,page,cookie)
except:
print("cookie格式错误,请重新输入")
return
import gui_rc
def gui_open():
app = QtWidgets.QApplication(sys.argv)
Time = QtWidgets.QWidget()
ui = Ui_Time()
ui.setupUi(Time)
Time.show()
sys.exit(app.exec_())
if __name__ == "__main__":
gui_open() |