博客
关于我
拉钩爬取部分重写
阅读量:465 次
发布时间:2019-03-06

本文共 3407 字,大约阅读时间需要 11 分钟。

拉钩重写:

1. 实现方式:

  • scrapy+selenium

  • 实现scrapy中的spider即可

2. 实现目标:

  • 为实现对接之前的公司项目模板,实现统一化

3. 实现思路:

  • 对关键字进行汉字转字母,进行URL拼接操作,然后请求;

  • 通过selenium获取到网页源码,进行信息解析;

  • yield返回给item,进行后续模板操作

 

4. 解决对关键字进行汉字转字母:

1  from pypinyin import lazy_pinyin2  a = lazy_pinyin("南京")3  print(a[0])4  ​5  print(a[1])6  #字符串拼接7  print(a[0]+a[1])

 

5. 结果:

1  nan2  jing3  nanjing

 

6. spider核心代码:

 

1 # -*- coding: utf-8 -*- 2  import scrapy 3  from selenium import webdriver 4  from selenium.webdriver import  ActionChains 5  import time 6  from pypinyin import lazy_pinyin 7  from TZtalent.items import TztalentItem 8  from lxml import etree 9  class LagouproSpider(scrapy.Spider):10      name = 'lagoupro'11      # allowed_domains = ['www.xxx.com']12      # start_urls = ['https://www.lagou.com/']13  ​14      def __init__(self, table_name, keyword, site, webhook, *args, **kwargs):15          super(LagouproSpider, self).__init__(*args, **kwargs)16          path = r"C:\Users\Administrator\Desktop\phantomjs-1.9.2-windows\phantomjs.exe"17          # self.driver = webdriver.PhantomJS(executable_path=path)18          # 防止selenium识别19          options = webdriver.ChromeOptions()20          options.add_experimental_option("excludeSwitches", ["enable-automation"])21          options.add_experimental_option('useAutomationExtension', False)22          self.driver = webdriver.Chrome(options=options)23          self.driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {24              "source": """25              Object.defineProperty(navigator, 'webdriver', {26                get: () => undefined27              })28            """29          })30          # self.driver = webdriver.Chrome()31          self.keyword = keyword32          self.webhook_url = webhook33          self.table_name = table_name34          #中文转拼音35          pinyin = lazy_pinyin(site)36          print(pinyin)37          self.site = pinyin[0]+pinyin[1]38          print(self.site)39          #字符串拼接---得到地域URL40          self.start_urls =[f"https://www.lagou.com/{self.site}-zhaopin/"]41  ​42  ​43      def parse(self, response):44          self.driver.find_element_by_id("keyword").send_keys(self.keyword)45          #鼠标移动到点击位置46          ac = self.driver.find_element_by_id("submit")47          ActionChains(self.driver).move_to_element(ac).perform()48          time.sleep(2)49          ActionChains(self.driver).move_to_element(ac).click(ac).perform()50          time.sleep(2)51          # 解析selenium发过来的response数据52          str_html= self.driver.page_source53          html = etree.HTML(str_html)54          try:55              # 父标签---所需要信息标签上的父标签56              div_list = html.xpath("//ul[@class='item_con_list']/li")57              item = TztalentItem()58              for div in div_list:59                  item['title'] = div.xpath(".//h3/text()")[0]60                  # 判断title是否为空61                  if item['title'] == None:62                      continue63                  item['company_name'] = div.xpath(".//div[@class='company_name']/a/text()")[0]64                  item['company_url'] = div.xpath(".//div[@class='company_name']/a/@href")[0]65                  item['site'] = div.xpath(".//span[@class='add']/em//text()")[0]66                  yield item67                  # print(item)68  ​69          except:70              print('没有数据')71  ​72      def spider_close(self, spider):73          # 退出驱动并关闭所有关联的窗口74          self.driver.quit()

 

 

转载地址:http://hfcbz.baihongyu.com/

你可能感兴趣的文章
MySQL中UPDATE语句的神奇技巧,让你操作数据库如虎添翼!
查看>>
Mysql中varchar类型数字排序不对踩坑记录
查看>>
MySQL中一条SQL语句到底是如何执行的呢?
查看>>
MySQL中你必须知道的10件事,1.5万字!
查看>>
MySQL中使用IN()查询到底走不走索引?
查看>>
Mysql中使用存储过程插入decimal和时间数据递增的模拟数据
查看>>
MySql中关于geometry类型的数据_空的时候如何插入处理_需用null_空字符串插入会报错_Cannot get geometry object from dat---MySql工作笔记003
查看>>
mysql中出现Incorrect DECIMAL value: '0' for column '' at row -1错误解决方案
查看>>
mysql中出现Unit mysql.service could not be found 的解决方法
查看>>
mysql中出现update-alternatives: 错误: 候选项路径 /etc/mysql/mysql.cnf 不存在 dpkg: 处理软件包 mysql-server-8.0的解决方法(全)
查看>>
Mysql中各类锁的机制图文详细解析(全)
查看>>
MySQL中地理位置数据扩展geometry的使用心得
查看>>
Mysql中存储引擎简介、修改、查询、选择
查看>>
Mysql中存储过程、存储函数、自定义函数、变量、流程控制语句、光标/游标、定义条件和处理程序的使用示例
查看>>
mysql中实现rownum,对结果进行排序
查看>>
mysql中对于数据库的基本操作
查看>>
Mysql中常用函数的使用示例
查看>>
MySql中怎样使用case-when实现判断查询结果返回
查看>>
Mysql中怎样使用update更新某列的数据减去指定值
查看>>
Mysql中怎样设置指定ip远程访问连接
查看>>