爬虫练手学习
1.模拟登陆 + 获取Cookie
2.修改Cookie数据格式
3.Requests库使用Cookie
4.正则表达式提取信息
from selenium import webdriverimport timeimport requestsimport reheaders = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36'}# 1.模拟登陆 + 获取Cookiebrowser = webdriver.Chrome()url = '/member/login.jhtml'browser.get(url)# browser.find_element_by_xpath('//*[@id="login"]/div[1]/i').click() # 通过这行代码可以自动切换成二维码模式,其实手动点也可以time.sleep(20) # 留20秒或者更长的时间来手动进行登录;推荐扫码登陆cookies = browser.get_cookies() # 获取Cookie# 2.修改Cookie数据格式cookie_dict = {}for item in cookies:cookie_dict[item['name']] = item['value']# 3.Requests库使用Cookieurl = '/search?q=王宇韬'res = requests.get(url, headers=headers, cookies=cookie_dict).text# 验证是否登录成功if 'fgwyt94' in res:print('登录成功')# 4.正则表达式提取信息title = re.findall('"raw_title":"(.*?)"', res)price = re.findall('"view_price":"(.*?)"', res)sale = re.findall('"view_sales":"(.*?)人付款"', res)for i in range(len(title)):print(title[i] + ',价格为:' + price[i] + ',销量为:' + sale[i])