【问题标题】:Ruby Nokogiri Scraper cannot remove HTML h1 tagsRuby Nokogiri Scraper 无法删除 HTML h1 标签
【发布时间】:2018-11-30 16:53:21
【问题描述】:

我觉得很愚蠢,因为它看起来很容易。但我坚持这个:

我建立了一个刮板,它可以让我获得工作的标题。 效果很好,但它包含 h1 标签。例如。它将工作的标题保存为:

"h1>营销经理/h1>" 我不知道他为什么不只取 h1 标签内的值。

但其次,我只是试图通过剥离标题的前 4 个和后 5 个字符(标题(4..-5))来剥离标签。不幸的是,没有像 strip 这样的函数起作用(错误告诉我它一些无法剥离的奇怪 nokogiri 类)。

这是我的代码,希望有人知道我的问题的智能解决方案:

company_career_urls.each do |url|
  puts "gets job url"
  # get the specific job url
  html_file = open(url).read
  html_doc = Nokogiri::HTML(html_file)
  i = 0
  Vacancy.where(:companyname => "Lillydoo").destroy_all
  html_doc.search('.job-list-button a').each do |element|
    i = i+1
    if i > 7
    else
      job_url = element.attribute('href').value
      puts job_url
      #get the job name and description
      html_file = open(job_url).read
      html_doc = Nokogiri::HTML(html_file)
      job_description = html_doc.search('.inner ul')
      job_title = html_doc.search('.job-detail-desc h1') #this line seems to be the problem
      # job_title = job_title_html[4..-6]
      puts job_title

      resource_type = "image"
      type = "upload"
      version = 1234567890
      public_id = "wv7l1o6xwimtfvx2oxdw"
      format = "jpg"
      signature = Cloudinary::Utils.api_sign_request({:public_id=>public_id,
      :version=>version}, Cloudinary.config.api_secret)
      photo = "#{resource_type}/#{type}/v#{version}/#{public_id}.#{format}##{signature}"
      vacancy = Vacancy.create(title: job_title, companyname: 'Lillydoo', jobdescription: job_description, photo: photo)
    end
  end

【问题讨论】:

  • 你可以试试html_doc.css(".job-detail-desc h1").text.strip
  • 刚刚做了。它仍然包含标签。它真的很棘手
  • 你可以在选择元素时发布实际打印的内容

标签: ruby-on-rails ruby web-scraping nokogiri


【解决方案1】:

这给了你一堆元素:

job_title = html_doc.search('.job-detail-desc h1')

这会给你第一个的文本:

job_title = html_doc.at('.job-detail-desc h1').text

【讨论】:

    【解决方案2】:

    您遇到的问题是job_title 不是一个简单的字符串;它是一组与搜索匹配的节点对象。当您使用puts 打印它时,Ruby 正在节点集上调用#to_s 并输出所有节点的“HTML 源代码”。

    您需要做的是隔离您想要的节点,然后使用#content(或#text)提取其文本内容。这是一个例子:

    require 'nokogiri'
    
    CONTENT = <<'EOT'
    <html>
      <body>
        <h1>Test Heading</h1>
      </body>
    </html>
    EOT
    
    html_doc = Nokogiri::HTML(CONTENT)
    
    # this returns a set of all matching nodes
    nodes = html_doc.css('h1')
    puts nodes.class   # --> "Nokogiri::XML::NodeSet"
    puts nodes         # --> "<h1>Test Heading<h1>"
    
    # if you know you will only have one, use at_css
    node = html_doc.at_css('h1')
    puts node.class    # --> "Nokogiri::XML::Element"
    puts node          # --> "<h1>Test Heading</h1>"
    
    # to get just the text content inside the node
    puts node.content  # --> "Test Heading"
    

    https://www.nokogiri.org/tutorials/searching_a_xml_html_document.html

    【讨论】:

      【解决方案3】:

      对于 HTML,经验法则是文档具有 htmlbody 标签,而片段通常没有。尝试使用 DocumentFragment 类,因为该文本不是有效的 HTML 或 XML 文档。

      html_doc = Nokogiri::HTML::DocumentFragment.parse(html_file)
      

      【讨论】:

      • 嗯,它仍然在字符串中包含 h1 标签。我刚试过。
      • 没有迹象表明 OP 正在解析文档片段。文档已被解析;问题是从节点中提取文本内容。
      猜你喜欢
      • 2021-02-10
      • 2012-07-18
      • 1970-01-01
      • 2013-08-01
      • 2012-03-10
      • 2014-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多