Use SELENIUM+PHANTOMJS crawl Hook net job information, save in CSV file to local Disk
Hook Net Job page, click on the next page, job information loading, but the URL of the browser is unchanged, indicating that the data is not sent GET request.
We're not going to look for its API. Here's another way: use PHANTOMJS to simulate browsing and get to the next page by clicking on the page.
The PHANTOMJS here is a browser with no interface.
1 fromSeleniumImportWebdriver2 Import Time3 ImportRandom4 5 fromSelenium.webdriver.common.byImport by6 fromSelenium.webdriver.supportImportExpected_conditions as EC7 fromSelenium.webdriver.support.uiImportwebdriverwait8 9 " "Ten use SELENIUM+PHANTOMJS crawl hook net job information, save to CSV file to local disk One need to add a request header A " " - - the " " - phantomjs.page.customHeaders.: Custom request Header fixed notation - For Example: Define proxy: Phantomjs.page.customheaders.user-agent - " " +DC = { - 'phantomjs.page.customheaders.user-agent':'mozilla/5.0 (Windows NT 10.0; Win64; x64) applewebkit/537.36 (khtml, like Gecko) chrome/63.0.3239.132 safari/537.36' + } A at #Create a Browser object -Browser = Webdriver. PHANTOMJS (executable_path=r'E:\PycharmProjects\pachong\phantomjs-2.1.1-windows\phantomjs-2.1.1-windows\bin\phantomjs.exe', desired_capabilities=DC) - - #Send Request -Browser.get ('https://www.lagou.com/jobs/list_?labelWords=&fromSearch=true&suginput=') -Time.sleep (2) in - #Save Web page toBrowser.save_screenshot ('Lagou.png') + - #instantiating the Wait object setting waiting time-out is 20 seconds thewait = webdriverwait (browser,20) * $ ## Create a CSV filePanax Notoginsengf = open ('Lagou.csv','W', encoding='Utf-8') - the whileTrue: + #Get Data AJob_list = Browser.find_elements_by_css_selector ('. item_con_list Li') the forJobinchjob_list: +PName = Job.find_element_by_tag_name ('H3'). Text -Ptime = Job.find_element_by_class_name ('Format-time'). Text $Company = Job.find_element_by_css_selector ('. Company_Name a'). Text $Money = Job.find_element_by_class_name (' Money'). Text -Exp = Job.find_element_by_class_name ('li_b_l'). Text.split (' ') [1]#the text here does not take the contents of the tag inside the Div, only the contents of the Div. There are two div classes named li_b_l, and experience is the second one, and there is a job requirement -Location = Job.find_element_by_tag_name ('em'). Text theReqtags = Job.find_elements_by_css_selector ('. li_b_l span')#the Div class is li_b_l, which contains a lot of span tags. The one that will pay is also included in the back, need to deal with -Reqtags =' '. Join ([Reqtag.text forReqtaginchReqtags][1:])#The first item in each entry is the one on the payroll, which is removed using a list of slices. Wuyi the #put data into a list for later CSV file format processing, using, separating each item -data =[Pname,ptime,company,money,exp,location,reqtags] Wu #print (data) -F.write (','. Join (data) +'\ n') About $ Print(data) - if 'Pager_next pager_next_disabled' not inchBrowser.page_source: - #Get Next page button -Wait.until (Ec.element_to_be_clickable (By.class_name,'Pager_next')))#There is a space at the end of the class in the original Web page A #Click to go to the next page +Browser.find_element_by_class_name ('Pager_next'). Click () theTime.sleep (3 + random.random () *)#time needs to be extended a little, time is too short, the page does not load complete, get data will be error - Else: $ Break the the #Close File theF.close ()
Selelinum+phantomjs Crawl Hook Net Job