马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 孟婆汤 于 2020-8-6 09:49 编辑 # coding=utf-8
import requests as rs
import re
from selenium import webdriver
from urllib import parse
import time
from selenium.webdriver.common.keys import Keys
class baidu:
def __init__(self, name, num):
self.index_url=''
self.headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KH'\
'TML, like Gecko) Chrome/83.0.4103.116 Safari/537.36'
}
self.name = name
self.index_num = num
self.wd = webdriver.Chrome(executable_path='chromedriver')
# 设置 浏览器最大化
self.wd.maximize_window()
self.wd.implicitly_wait(10)
def run(self):
# 合并 各种网址
self.combine_url()
# 获取 帖子地址
self.get_index_url_list()
# 百度登录
self.login_baidu()
# 开始顶贴
self.up_title()
def combine_url(self):
temp_url = 'https://tieba.baidu.com/f?'
self.index_url = temp_url + parse.urlencode({'kw' : self.name})+'&ie=utf-8&pn=0'
self.title_basic_href = 'https://tieba.baidu.com/p/'
self.title_href_list = []
def get_index_url_list(self):
for i in range(self.index_num):
self.index_url = self.index_url[:-1]+f'{i*50}'
con = rs.get(url=self.index_url,headers=self.headers).content.decode('utf-8')
href_list = re.findall('回复">.*?</span>[\s\S]*?href="/p/(.*?)"\stitle',con)
self.title_href_list.append(href_list)
def login_baidu(self):
url = 'https://passport.baidu.com/v2/?login'
self.wd.get(url=url)
self.wd.find_element_by_id('TANGRAM__PSP_3__footerULoginBtn').click()
time.sleep(0.1)
# 输入 账号
self.wd.find_element_by_id('TANGRAM__PSP_3__userName').send_keys('贴吧账号')
# 输入 密码
self.wd.find_element_by_id('TANGRAM__PSP_3__password').send_keys('贴吧密码')
# 去除 下次登录
self.wd.find_element_by_id('TANGRAM__PSP_3__memberPass').click()
time.sleep(0.1)
# 登录
self.wd.find_element_by_id('TANGRAM__PSP_3__submit').click()
while True:
# 判断 是否跳转,如果为否,则判断是否存在弹出框
if url == self.wd.current_url:
time.sleep(0.2)
try:
self.wd.find_element_by_id('TANGRAM__PSP_21__foreground').find_element_by_id('TANGRAM__PSP_27__jump').click()
break
except:
time.sleep(0.2)
continue
# 已经跳转,则退出
else:
break
def up_title(self):
for i in self.title_href_list:
for j in i:
self.wd.get(self.title_basic_href+j)
time.sleep(0.1)
js = 'scrollTo(0,100000)'
self.wd.execute_script(js)
p = self.wd.find_element_by_id('ueditor_replace').find_element_by_xpath('./p')
p.send_keys('46846464684684')
p.send_keys(Keys.CONTROL,Keys.ENTER)
# 最后再加一个 判断 是否顶贴成功即可
# 下午 再干
# 拖了一周都没干。。。唉 拖延症
def main():
bd = baidu('灌水',1)
bd.run()
if __name__ == '__main__':
main()
|