【发布时间】: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