爬取目标
1.本次代码是在python2上运行通过的,python3的最需改2行代码,用到其它python模块
selenium 2.53.6 +firefox 44
BeautifulSoup
requests
2.爬取目标网站,我的博客:https://home.cnblogs.com/u/yoyoketang 爬取内容:爬我的博客的所有粉丝的名称,并保存到txt
3.由于博客园的登录是需要人机验证的,所以是无法直接用账号密码登录,需借助selenium登录
selenium获取cookies
1.大前提:先手工操作浏览器,登录我的博客,并记住密码 (保证关掉浏览器后,下次打开浏览器访问我的博客时候是登录状态) 2.selenium默认启动浏览器是一个空的配置,默认不加载配置缓存文件,这里先得找到对应浏览器的配置文件地址,以火狐浏览器为例 3.使用driver.get_cookies()方法获取浏览器的cookies
# coding:utf-8
import requests
from selenium import webdriver
from bs4 import BeautifulSoup
import re
import time
# firefox浏览器配置文件地址
profile_directory = r'C:\Users\admin\AppData\Roaming\Mozilla\Firefox\Profiles\yn80ouvt.default'
# 加载配置
profile = webdriver.FirefoxProfile(profile_directory)
# 启动浏览器配置
driver = webdriver.Firefox(profile)
driver.get("https://home.cnblogs.com/u/yoyoketang/followers/")
time.sleep(3)
cookies = driver.get_cookies() # 获取浏览器cookies
print(cookies)
driver.quit()
(注: