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

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

拉钩重写是实现数据对接和统一化的重要步骤,以下是实现拉钩重写的详细思路和方法。

实现方式

  • Scrapy + Selenium结合使用

    • 使用Scrapy作为网页抓取框架,处理静态内容。
    • 使用Selenium处理动态加载的内容,模拟用户操作,获取完整网页源码。
  • Scrapy Spider实现

    • 编写Scrapy的爬虫类,继承自scrapy.Spider。
    • 在爬虫类中,初始化相关参数,如关键字、站点、 webhook URL 等。
    • 通过pypinyin库将中文关键字转换为拼音,生成URL地址。
  • 实现目标

  • 数据对接统一
    • 将数据规范化,适配之前的项目模板。
    • 实现数据的无缝对接,提升数据处理效率。
  • 实现思路

  • 关键字处理

    • 使用pypinyin库将中文关键字转换为拼音。
    • 拼接生成URL地址,例如“南京”转换为“nanjing”,生成“https://www.lagou.com/nanjing-zhaopin/”。
  • 网页请求

    • 使用Selenium模拟浏览器操作,访问生成的URL。
    • 设置浏览器选项,避免被网站检测,确保请求成功。
  • 网页解析

    • 使用Scrapy框架和lxml库解析网页内容。
    • 定位所需的信息标签,提取公司名称、职位信息等数据。
  • 数据存储

    • 将提取的信息存储到数据库中,供后续模板处理使用。
  • 关键字转换示例

    from pypinyin import lazy_pinyin2pinyin = lazy_pinyin2("南京")  # 返回列表 ['nan', 'jing']print(pinyin[0])  # 输出 'nan'print(pinyin[1])  # 输出 'jing'print(pinyin[0] + pinyin[1])  # 输出 'nanjing'

    Spider核心代码

    from scrapy import Spiderfrom selenium import webdriverfrom selenium.webdriver.chrome.options import Optionsfrom time import sleepfrom pypinyin import lazy_pinyin2from lxml import etreefrom Tztalent.items import TztalentItemclass LagouproSpider(Spider):    name = 'lagoupro'        def __init__(self, table_name, keyword, site, webhook):        super(LagouproSpider, self).__init__()        # 设置浏览器选项,避免被检测        options = Options()        options.add_experimental_option("excludeSwitches", ["enable-automation"])        options.add_experimental_option('useAutomationExtension', False)        self.driver = webdriver.Chrome(options=options)        # 运行脚本,禁用浏览器自动化检测        self.driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {            "source": """Object.defineProperty(navigator, 'webdriver', { get: () => undefined })"""        })        self.keyword = keyword        self.webhook_url = webhook        self.table_name = table_name        # 转换站点为拼音        pinyin = lazy_pinyin2(site)        self.site = pinyin[0] + pinyin[1]        # 生成起始 URL        self.start_urls = [f"https://www.lagou.com/{self.site}-zhaopin/"]        def parse(self, response):        try:            # 找到搜索关键字框,输入关键字            self.driver.find_element_by_id("keyword").send_keys(self.keyword)            # 模拟点击搜索按钮            submit = self.driver.find_element_by_id("submit")            ActionChains(self.driver).move_to_element(submit).perform()            sleep(2)            ActionChains(self.driver).click(submit).perform()            sleep(2)            # 获取网页源码            str_html = self.driver.page_source            html = etree.HTML(str_html)            # 提取 job list 列表            job_list = html.xpath("//ul[@class='item_con_list']/li")            for job in job_list:                item = TztalentItem()                # 提取标题                item['title'] = job.xpath(".//h3/text()")[0]                # 提取公司名称和链接                company_info = job.xpath(".//div[@class='company_name']/a")                item['company_name'] = company_info[0].text                item['company_url'] = company_info[0].get('href')                # 提取地点                location = job.xpath(".//span[@class='add']/em/text()")[0]                item['site'] = location                yield item        except Exception as e:            print(f"Error: {e}")            print('没有数据')

    结果

    通过上述方法,可以实现对关键字的汉字转字母,并生成正确的URL,成功获取网页内容,提取所需信息。最终数据将存储到数据库中,供后续模板处理使用。

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

    你可能感兴趣的文章
    NOTE:rfc5766-turn-server
    查看>>
    Notepad ++ 安装与配置教程(非常详细)从零基础入门到精通,看完这一篇就够了
    查看>>
    Notepad++在线和离线安装JSON格式化插件
    查看>>
    notepad++最详情汇总
    查看>>
    notepad++正则表达式替换字符串详解
    查看>>
    notepad如何自动对齐_notepad++怎么自动排版
    查看>>
    Notes on Paul Irish's "Things I learned from the jQuery source" casts
    查看>>
    Notification 使用详解(很全
    查看>>
    NotImplementedError: Cannot copy out of meta tensor; no data! Please use torch.nn.Module.to_empty()
    查看>>
    NotImplementedError: Could not run torchvision::nms
    查看>>
    nova基于ubs机制扩展scheduler-filter
    查看>>
    Now trying to drop the old temporary tablespace, the session hangs.
    查看>>
    nowcoder—Beauty of Trees
    查看>>
    np.arange()和np.linspace()绘制logistic回归图像时得到不同的结果?
    查看>>
    np.power的使用
    查看>>
    NPM 2FA双重认证的设置方法
    查看>>
    npm build报错Cannot find module ‘webpack/lib/rules/BasicEffectRulePlugin‘解决方法
    查看>>
    npm build报错Cannot find module ‘webpack‘解决方法
    查看>>
    npm ERR! ERESOLVE could not resolve报错
    查看>>
    npm ERR! fatal: unable to connect to github.com:
    查看>>