Python crawler--The selenium analog landing +requests. Session () Get cookies

Source: Internet
Author: User

The code is as follows:

1 #Coding:utf-82  fromSeleniumImportWebdriver3 ImportRequests4 ImportSYS5 Import Time6  fromlxmlImportetree7 #Reload (SYS)8 #sys.setdefaultencoding (' utf-8 ')9 Ten classZhihu: One     def __init__(self,homeurl): ASelf.homeurl =Homeurl -  -     defgetcookies (self): theBrowser =Webdriver. Chrome () -Browser.get ("Https://www.zhihu.com/signin") -Browser.find_element_by_css_selector (". Signflow-accountinput.input-wrapper Input"). Send_keys ("13060882373") -Browser.find_element_by_css_selector (". Signflow-password input"). Send_keys ("XXXXXX") +Browser.find_element_by_css_selector (". Button.signflow-submitbutton"). Click () -Time.sleep (3) +         #js = "Window.scrollto (0, document.body.scrollHeight);" A         #browser.execute_script (JS) at         #Time.sleep (3) -cookies =browser.get_cookies () - browser.quit () -         returnCookies -  -     defCrawl (self): ins =requests. Session () - s.headers.clear () to          forCookiesinchSelf . GetCookies (): +S.cookies.set (cookie['name'], cookie['value']) -HTML =S.get (self.homeurl). Text theHtml_tree =etree. HTML (HTML) *Items = Html_tree.xpath ('//*[@id = "root"]/div/main/div/div/div[1]/div[2]/div//div[@class = "ContentItem answeritem"]/@data-zop') $          forIteminchItems:Panax Notoginseng             #Print Item -Content =eval (item) theAuthorName = content['AuthorName'] +title = content['title'] A             PrintAuthorName +"answered the following:"+title the  +  -Zhihu = Zhihu ('https://www.zhihu.com/') $Zhihu. Crawl ()

Python crawler--The selenium analog landing +requests. Session () Get cookies

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.