from selenium import webdriver
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.chrome.options import Options
import time
import re
def Login():
print('正在登陆。。。')
url = 'https://tieba.baidu.com/index.html'
cookies = [{'domain': '.tieba.baidu.com', 'expiry': 1609430400.29948, 'httpOnly': False, 'name': 'TIEBA_USERTYPE', 'path': '/', 'secure': False, 'value': '5e2f280bc78e36aeed33c8e2'}, {'domain': '.tieba.baidu.com', 'expiry': 1609430400.299452, 'httpOnly': False, 'name': 'TIEBAUID', 'path': '/', 'secure': False, 'value': 'cb23caae14130a0d384a57f1'}, {'domain': '.baidu.com', 'expiry': 1578558401.299503, 'httpOnly': False, 'name': 'BAIDUID', 'path': '/', 'secure': False, 'value': 'D9F7ED71E114482298100863E6AE5E04:FG=1'}, {'domain': '.tieba.baidu.com', 'expiry': 1578558401, 'httpOnly': False, 'name': 'Hm_lvt_98b9d8c2fd6608d564bf2ac2ae642948', 'path': '/', 'secure': False, 'value': '1547022402'}, {'domain': '.tieba.baidu.com', 'httpOnly': False, 'name': 'Hm_lpvt_98b9d8c2fd6608d564bf2ac2ae642948', 'path': '/', 'secure': False, 'value': '1547022402'}, {'domain': '.baidu.com', 'expiry': 1806222417.69828, 'httpOnly': True, 'name': 'BDUSS', 'path': '/', 'secure': False, 'value': 'G00a2djUTJzQTBOQmlqNWMtUEhFaXE5TThkd1BXfnE0c0VNVy1kSmFwMVFQVjFjQVFBQUFBJCQAAAAAAAAAAAEAAADf0~4swNfT6sDhAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFCwNVxQsDVcd'}]
options = Options()
options.add_argument('-headless')
browser = webdriver.Chrome(chrome_options=options)# browser.maximize_window()
wait = WebDriverWait(browser, 10)
browser.get(url)
for i in cookies:
browser.add_cookie(i)# 如果不设置Cookie会出现手机短信验证
login = wait.until(EC.element_to_be_clickable(
(By.CSS_SELECTOR, '#com_userbar > ul > li.u_login > div > a')
)).click()
username_login = wait.until(EC.element_to_be_clickable(
(By.CSS_SELECTOR, '#TANGRAM__PSP_10__footerULoginBtn')
)).click()
input_username = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, '#TANGRAM__PSP_10__userName')))
input_username.send_keys('这里是登录账号')
input_password = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, '#TANGRAM__PSP_10__password')))
input_password.send_keys('这里是登录密码')
submit = wait.until(EC.element_to_be_clickable(
(By.CSS_SELECTOR, '#TANGRAM__PSP_10__submit')
)).click()
return browser
def Enter_my_tieba(browser):
wait = WebDriverWait(browser, 10)
my_tieba = wait.until(EC.element_to_be_clickable(
(By.CSS_SELECTOR, '#j_u_username > div.u_menu_item.u_menu_username > a')
)).click()
tieba_page = wait.until(EC.element_to_be_clickable(
(By.CSS_SELECTOR, '#ihome_nav_wrap > ul > li:nth-child(4) > div > p > a')
)).click()
browser.close()# 关闭当前的标签页
browser.switch_to.window(browser.window_handles[0])# 切换到新出现的标签页
return browser
def Get_tieba_datalist(browser):
print('正在获取关注的贴吧信息。。。')
time.sleep(2)# 这里可能会卡
last_page = browser.find_element_by_link_text('尾页').get_attribute('href')
page = re.search('pn=(\d+)',last_page).group(1)
data = []
for i in range(0,int(page)-1):
tieba_data = Get_datalist(browser)
data.extend(tieba_data)
time.sleep(0.5)
next_page = browser.find_element_by_link_text('下一页').click()
for i in data:
Qiandao(i[0],i[1])
def Qiandao(name,url):
print('正在签到'+name+'吧。。。')
browser.get(url)
wait = WebDriverWait(browser, 10)
try:
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '#signstar_wrapper'))).click()# 先点击父级元素
qiandao = browser.find_element_by_css_selector('#signstar_wrapper > a')
qiandao.click()# 再点击一次
time.sleep(0.5)# 点击完等待一下
if qiandao.get_attribute('class')=='j_signbtn signstar_signed':# 如果样式变了,表示签到成功
print(name+'吧签到成功!')
except:# 有的吧可能进不去
print(name+'吧签到失败')
def Get_datalist(browser):
tieba_data = []
tr = browser.find_elements_by_css_selector('table > tbody > tr')
for i in tr[1:]:
a = i.find_element_by_css_selector('td:nth-child(1) > a')
grade = i.find_element_by_css_selector('td:nth-child(3) > a > div.like_badge_lv').text
data = (a.get_attribute('title'), a.get_attribute('href'), int(grade))
tieba_data.append(data)
return tieba_data
if __name__=='__main__':
browser = Login()
browser = Enter_my_tieba(browser)
Get_tieba_datalist(browser)
selenium贴吧签到
最新推荐文章于 2021-08-24 16:55:19 发布