selenium get cookie and added, requests processed cookie

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/lipachong/article/details/98197837

Today the world is trying to crawl room area, we found that requests access, not pages, selenium access code page will appear, try selenium plus a cookie after the visit, there is no verification code page, and page data obtained
1.selenium obtain and add cookie

import requests
from bs4 import BeautifulSoup
import time
from selenium import webdriver
for page in range(1,3):
    url='https://cd.esf.fang.com/housing/__0_0_0_0_{}_0_0_0/'.format(str(page))
    driver=webdriver.Chrome()
    driver.get(url)
    time.sleep(3)
    #获取所有cookie,是一个列表,可能是一个或多个字典形式cookie
    cookies=driver.get_cookies()
    print(cookies)
    cookie=cookies[0]
    print(cookie)

	

	driver.add_cookie(cookie)
	#此时携带者cookie的driver再次访问网址
    driver.get(url)
    time.sleep(3)
    page=driver.page_source
    print(page)

2, requests
before I deal with cookie problems with requssts like this:

session=requests.session()
rq=session.get(url,header=header)
....

But not this time, I do not know why. .
I am so lazy, to be out data on the line, with requests for reasons to get out of there Gangster know it, hoping to provide some grateful

Guess you like

Origin blog.csdn.net/lipachong/article/details/98197837