【问题标题】:Need to fetch the email id and phone number from web scraping [closed]需要从网络抓取中获取电子邮件 ID 和电话号码 [关闭]
【发布时间】:2020-08-24 05:47:24
【问题描述】:
require 'open-uri'
require 'nokogiri'

def scrape(url)
  html = open(url).read
  nokogiri_doc = Nokogiri::HTML(html)
  final_array = []

  nokogiri_doc.search("a").each do |element|
    element = element.text
    final_array << element
  end

  final_array.each_with_index do |index|
    puts "#{index}"
  end
end


scrape('http://www.infranetsol.com/')

在此我只获得a 标签,但我需要将电子邮件 ID 和电话号码放入 excel 文件中。

【问题讨论】:

  • “我需要特别是excel文件中的电子邮件ID和电话号码”这到底是什么意思?你是在用 Nokogiri 解析 excel 文件吗?

标签: ruby web-scraping web-crawler nokogiri open-uri


【解决方案1】:

你只有文字。所以,你能做的就是只保留看起来像电子邮件或电话号码的字符串。

例如,如果将结果保存在数组中

a = scrape('http://www.infranetsol.com/')

您可以通过电子邮件获取元素(带有“@”的字符串):

a.select { |s| s.match(/.*@.*/) }

您可以获取带有电话号码的元素(至少 5 位数字的字符串):

a.select{ |s| s.match(/\d{5}/) }

整个代码:

require 'open-uri'
require 'nokogiri'

def scrape(url)
  html = open(url).read
  nokogiri_doc = Nokogiri::HTML(html)
  final_array = []

  nokogiri_doc.search("a").each do |element|
    element = element.text
    final_array << element
  end

  final_array.each_with_index do |index|
    puts "#{index}"
  end
end


a = scrape('http://www.infranetsol.com/')
email = a.select { |s| s.match(/.*@.*/) }
phone = a.select{ |s| s.match(/\d{5}/) }

# in your example, you will have to email in email 
# and unfortunately a complex string for phone.
# you can use scan to extract phone from text and flat_map 
# to get an array without sub array
# But keep in mind it will only worked with this text

phone.flat_map{ |elt| elt.scan(/\d[\d ]*/) }

【讨论】:

  • 嗨@GPif你能写下整个代码,如果可能的话请发送谢谢
  • require 'open-uri' require 'nokogiri' def scrap(url) html = open(url).read nokogiri_doc = Nokogiri::HTML(html) final_array = [] nokogiri_doc.search("a ").每个都做 |元素| element = element.text final_array infranetsol.com/') a.select { |s| s.match(/.*@.*/) } a.select { |s| s.match(/\d{5}/) } 我试过这种方式,但结果是一样的。我得到了整个标签
  • require 'open-uri' 需要 'nokogiri' url = "grofers.com/cn/grocery-staples/cid/16" response = open(url).read parsed_data = Nokogiri::HTML(response) results = [] content = parsed_data。 css('.section-right').css('.products products--grid').each 做 |row| title = row.css('a.product__wrapper').text puts title results
猜你喜欢
  • 1970-01-01
  • 2014-02-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-01
  • 1970-01-01
  • 2015-07-06
  • 2018-04-09
相关资源
最近更新 更多