最近发现有人QQ空间对我展开了屏蔽,咱们也不知道怎么惹到人家了,一气之下写了一个小爬虫看看到底谁把我屏蔽了。写小本本记下来!!!
准备工作
python环境:
python3.7.4
第三方库环境:
requests
lxml
threadpool
selenium
利用selenium模拟登陆获取cookie并保存到本地
def search_cookie(): # 先检测一下是否运行过
if not __import__('os').path.exists('cookie_dict.txt'):
get_cookie_json()
with open('cookie_dict.txt', 'r') as f:
cookie=json.load(f)
return cookie
def get_cookie_json(): # 无头selenium登陆
qq_number = input('请输入qq号:')
password = __import__('getpass').getpass('请输入qq密码:')
from selenium import webdriver
login_url = 'https://i.qq.com/'
chrome_options =Options()
chrome_options.add_argument('--headless')
driver = webdriver.Chrome(options=chrome_options)
driver.get(login_url)
driver.switch_to_frame('login_frame')
driver.find_element_by_xpath('//*[@id="switcher_plogin"]').click()
time.sleep(1)
driver.find_element_by_xpath('//*[@id="u"]').send_keys(qq_number)
driver.find_element_by_xpath('//*[@id="p"]').send_keys(password)
time.sleep(1)
driver.find_element_by_xpath('//*[@id="login_button"]').click()
time.sleep(1)
cookie_list = driver.get_cookies()
cookie_dict = {}
for cookie in cookie_list:
if 'name' in cookie and 'value' in cookie:
cookie_dict[cookie['name']] = cookie['value']
with open('cookie_dict.txt', 'w') as f:
json.dump(cookie_dict, f)
return True
找到查看好友的接口
进入我的空间,点击 F12 检查界面,将 Network 清空后点击好友界面。
首选盲猜好友列表含有friend字段。直接选择搜索发现出来一些数据,挨个查找之后发现好友字段。保存当前获得的 url 供日后查询。
破解data里面的加密参数