1. Normal tirón de cookies web hook datos de rastreo con AJAX
las solicitudes de importación de importación etree lxml tiempo de importación import re cabeceras = { "User-Agent": "Mozilla / 5.0 (Windows NT 10,0; WOW64) AppleWebKit / 537.36 (KHTML, like Gecko) Chrome / 65.0.3325.181 Safari / 537.36", " árbitro ": "https://www.lagou.com/jobs/list_python?labelWords=&fromSearch=true&suginput=", LG_LOGIN_USER_ID = 5b895ff2a4e23c48dc4c9110a6a1361bbf709630b5b17ac6756340fef1babfbf; LG_HAS_LOGIN = 1; showExpriedIndex = 1; showExpriedCompanyHome = 1; showExpriedMyPublish = 1; hasDeliver = 0; privacyPolicyPopup = false; Hm_lvt_4233e74dff0ae5bd0a3d81c6ccf756e6 = 1583857959,1583912708,1583912713; JSESSIONID = ABAAAECABGFABFF1412C84500FD39A23D7C1D5172179D66; WEBTJ-ID = 20200315123348-170dc782d0e4cf-05e9fb23740e5e-3a614f0b-2073600-170dc782d0f63d; _gid = GA1.2.1720707822.1584246829; _putrc = 387928C58CE0A7D1123F89F2B170EADC; entrada = true; Unick =% E7% 90% B3% E7% 90% B3; TG-TRACK-CODE = index_search; X_MIDDLE_TOKEN = 0a8830791829a77f99654a1bb3d568ae; LGSID = 20200315140707-568ce08c-c655-44b2-9cd4-66632e1bb6f4; PRE_UTM =; PRE_HOST =; PRE_SITE = https% 3A% 2F% 2Fwww.lagou.com; PRE_LAND = https% 3A% 2F% 2Fwww.lagou. com% 2Fjobs% 2Flist% 5Fpython% 2FP-ciudad% 5F0% 3F% 26CL% 3Dfalse% 26fromSearch% 3Dtrue% 26labelWords% 3D% 3D 26suginput%; _gat = 1; SEARCH_ID = 79abbbd66c2b4a59b7ca19ee8fb77e01; X_HTTP_TOKEN = 9944cc335d13b0d30552524851b568c7665cd1a0ff; LGRID = 20200315140911-acf5dfc4-1c8f-4943-a93f-983d364a96db", "Origen": "https://www.lagou.com", 'X-Anit-Forge-Code': "0", "X-Forge -Anit-Token": "Ninguno", "X-solicitada-Con ": "XMLHttpRequest" } posiciones = [] def request_list_page (): url = "https://www.lagou.com/jobs/positionAjax.json?needAddtionalResult=false" data = { "Frist": "false", " pn ": "1", "kd": "pitón" } para X en la gama (1,10): datos [ 'pn'] = x respuesta = requests.post (url, data = datos, cabeceras = cabeceras) resultado = respuesta.json () devuelve JSON # si los datos son para cargar un diccionario posiciones = resultado de [ 'contenido'] [ 'positionResult'] [ 'resultado'] para la posición en posiciones:tira () ciudad = re.sub (r "[\ s /]", "", de la ciudad) positionId = posición [ 'positionId'] #根据这个Identificación del找页面 position_url = 'https: //www.lagou.com/jobs/%s.html'%positionId parse_position_detail (position_url) descanso time.sleep (2) rotura def parse_position_detail (URL): respuesta = requests.get (url, cabeceras = cabeceras) text = response.text html = etree.HTML (texto) name = html.xpath ( "// div [@ class = 'trabajo-name'] / @title ") [0] job_span = html.xpath (" // dd [@ class = 'job_request'] // lapso ") salario = job_span [0] .xpath ( '.// texto ()') [0 ] .strip () ciudad = job_span [1] .xpath ( ".// texto ()") [0] .strip () posición = { 'nombre nombre, 'sueldo': el sueldo, 'ciudad': ciudad } positions.append (posición) def main (): request_list_page () de impresión (posiciones) si __name__ == '__main__': main ()
2. selenio datos de rastreo Gancho de tiro Web Ajax
#encoding: UTF-8 de WebDriver importación de selenio a partir lxml etree importación import re tiempo de importación de selenium.webdriver.support.ui WebDriverWait importación de importación expected_conditions selenium.webdriver.support como EC de importación selenium.webdriver.common.by Por clase LagouSpider (objeto): def __init __ (self): self.driver = webdriver.Firefox () self.url = 'https://www.lagou.com/jobs/list_python?labelWords=&fromSearch=true&suginput=' self.positions = [ ] def ejecutar (self): self.driver.get (self.url) while True: fuente = self.driver.page_source WebDriverWait (driver = self.driver, timeout = 10) .hasta ( EC.presence_of_element_located ((By.XPATH, "div [@ class = 'pager_container'] / span [last ()]]")) ) self.parse_list_page ( fuente) Proveedores: next_btn = self.driver.find_element_by_xpath ( "// div [@ class = 'pager_container'] / span [last ()]") si "pager_next_disabled" en next_btn.get_attribute ( "clase"): ruptura otra cosa: next_btn.click () excepto: impresión (fuente) time.sleep (1) def parse_list_page (self, fuente): html = etree.HTML (fuente) links = html.xpath ( "// a [@ class = 'position_link'] / @ href") ) Para el enlace en los enlaces: self.request_detail_page (enlace) time.sleep (1) def request_detail_page (auto, url): # self.driver.get ( url) self.driver.execute_script ( "window.open ( '% s')" % url) #打开新标签 self.driver.switch_to.window (self.driver.window_handles [1]) # conductor移动到新标签 WebDriverWait (self.driver, timeout = 10) .hasta ( EC.presence_of_element_located ((By.XPATH, "// div [@ class = 'trabajo-name']")) ) fuente = self.driver.page_source self.parse_detail_page ( fuente) self.driver.close () #关闭当前这个详情页 self.driver.switch_to.window (self.driver.window_handles [0]) #继续切换回职位列表页 def parse_detail_page (self, fuente): html = etree.HTML (fuente) name = html.xpath ( "// div [@ class = 'trabajo-name'] / @ title ") [0] job_span = html.xpath (" // dd [@ class = 'job_request'] // lapso ") salario = job_span [0] .xpath ( '.// texto ()') [0] .strip () ciudad = job_span [1] .xpath (" .// texto () ") [0] .strip () ciudad = re.sub (r"[ ]", "", \ s / ciudad) posición = { 'nombre': nombre, 'sueldo': el sueldo, 'ciudad': ciudad } self.positions.append (posición) de impresión (posición) de impresión ( '=' * 40) si __name__ == '__main__': araña = LagouSpider () spider.run ()