由于微博各种验证码,这里使用手动过验证
from selenium import webdriver
import time
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from cunchu import Redisclient
import json
class new_cookies():
def __init__(self):
self.account_db=Redisclient('account','weibo')
self.cookies_db=Redisclient('cookies','weibo')
def duquzhanghao(self):
users=self.account_db.username()
for user in users:
yield user
def baocun(self,username,cookies):
self.cookies_db.set(username,json.dumps(cookies))
#driver=webdriver.Chrome()
def n_cookies(self, username):
uuser=username
pas=self.account_db.get(uuser)
driver=webdriver.Chrome()
driver.delete_all_cookies()
time.sleep(1)
driver.get('https://weibo.com/')
#time.sleep(3)
WebDriverWait(driver,20,0.5).until(EC.visibility_of_element_located((By.ID,'loginname'))).send_keys(uuser)
driver.find_element_by_name('password').send_keys(pas)
driver.find_element_by_xpath('//*[@id="pl_login_form"]/div/div[3]/div[6]/a/span').click()
time.sleep(1)
WebDriverWait(driver,100,0.5).until(EC.visibility_of_element_located((By.CLASS_NAME,'B_index')))
cookies = driver.get_cookies()
#ret={}
#for cookie in cookies:
# cookie_name=cookie['name']
# cookie_val=cookie['value']
# ret=cookie_name+'='+cookie_val
self.baocun(uuser,cookies)
driver.close()
def main(self):
duqu=self.duquzhanghao()
for x in duqu:
print(x)
if not x in self.cookies_db.username():
print(f'正在生成cookies...用户名为:{x}')
self.n_cookies(x)
if __name__ == '__main__':
start=new_cookies()
start.main()
pc版微博使用cookie登录
import requests
from bs4 import BeautifulSoup
import re
headers = {
'Cookie':'WBStorage=6b696629409558bc|undefined;Ugrow-G0=d52660735d1ea4ed313e0beb68c05fc5;wb_view_log_7125808863=2560*14401;login_sid_t=3ad62385718b3b5b1f92ac972379c12a;cross_origin_proto=SSL;ULV=1560774064175:1:1:1:2508389041312.6465.1560774064170:;YF-V5-G0=125128c5d7f9f51f96971f11468b5a3f;YF-Page-G0=761bd8cde5c9cef594414e10263abf81|1560774070|1560774070;_s_tentry=passport.weibo.com;SINAGLOBAL=2508389041312.6465.1560774064170;Apache=2508389041312.6465.1560774064170;wb_view_log=2560*14401;SUHB=0vJYvuxB_LFc8r;SUBP=0033WrSXqPxfM725Ws9jqgMF55529P9D9W5UBHJaQozGsMlSa0_yQKpd5JpX5K2hUgL.FoMpeo-RehnRSoe2dJLoI7vWIPiPgH2LxK-LBo5LBo2t;ALF=1592310065;SSOLoginState=1560774066;SCF=AqXCTaf0OiHe7c5TAy3Ozm2IvbB4YZEjzGfCIotUE62mNpFvjgMpN9PYSJIdju5-GORERsB_-UGmARMFDAxLMhA.;SUB=_2A25wA_XlDeThGeFP6VcZ8CbEzT-IHXVTeWAtrDV8PUNbmtAKLWvfkW9NQTkdOjOcgSBwIu43ODBkrMLcDNwT_KIV;un=edx7pv7wb5@gggggs.fun;wvr=6;webim_unReadCount=%7B%22time%22%3A1560774074466%2C%22dm_pub_total%22%3A0%2C%22chat_group_pc%22%3A0%2C%22allcountNum%22%3A52%2C%22msgbox%22%3A0%7D',
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36 Edge/15.15063'}
#urls= headers['Cookie']
#sss=re.findall(r'wb_view_log_(.+?)=',urls)
#print(sss)
url='https://weibo.com'
print(url)
cookies='WBStorage=6b696629409558bc|undefined;Ugrow-G0=d52660735d1ea4ed313e0beb68c05fc5;wb_view_log_7125808863=2560*14401;login_sid_t=3ad62385718b3b5b1f92ac972379c12a;cross_origin_proto=SSL;ULV=1560774064175:1:1:1:2508389041312.6465.1560774064170:;YF-V5-G0=125128c5d7f9f51f96971f11468b5a3f;YF-Page-G0=761bd8cde5c9cef594414e10263abf81|1560774070|1560774070;_s_tentry=passport.weibo.com;SINAGLOBAL=2508389041312.6465.1560774064170;Apache=2508389041312.6465.1560774064170;wb_view_log=2560*14401;SUHB=0vJYvuxB_LFc8r;SUBP=0033WrSXqPxfM725Ws9jqgMF55529P9D9W5UBHJaQozGsMlSa0_yQKpd5JpX5K2hUgL.FoMpeo-RehnRSoe2dJLoI7vWIPiPgH2LxK-LBo5LBo2t;ALF=1592310065;SSOLoginState=1560774066;SCF=AqXCTaf0OiHe7c5TAy3Ozm2IvbB4YZEjzGfCIotUE62mNpFvjgMpN9PYSJIdju5-GORERsB_-UGmARMFDAxLMhA.;SUB=_2A25wA_XlDeThGeFP6VcZ8CbEzT-IHXVTeWAtrDV8PUNbmtAKLWvfkW9NQTkdOjOcgSBwIu43ODBkrMLcDNwT_KIV;un=edx7pv7wb5@gggggs.fun;wvr=6;webim_unReadCount=%7B%22time%22%3A1560774074466%2C%22dm_pub_total%22%3A0%2C%22chat_group_pc%22%3A0%2C%22allcountNum%22%3A52%2C%22msgbox%22%3A0%7D'
#如使用r=requests.get(url,headers=headers,cookies=cookies)带cookies参数的形式,需要将cookies转化为字典形式
#cookies = {i.split("=")[0]:i.split("=")[1] for i in cookies.split(";")}
#r=requests.get(url,headers=headers,cookies=cookies)
r=requests.get(url,headers=headers)
soup=BeautifulSoup(r.text,'html.parser')
print(soup)