我尝试从HTML页面获取电话,但是我尝试了许多左右左右和模块,但它不起作用或错误。示例。
http://www.boat-duesseldorf.com/cgi-bin/md_boot/lib/pub/tt.cgi/Overview_Press.html?oid=58288&lang=2&ticket=g_u_e_s_t
您可以在页面上看到很多电话。 49(0(211 4560-518。我从Regexp Base找到了很好的Regexp,Google推荐它。
http://www.regexlib.com/REDetails.aspx?regexp_id=2054
有很多文字,所以我不会在这里经过它。只有REGEXP。REGEXP应获得电话格式。
^(+d{2}[ -]{0,1}){0,1}(((({0,1}[ -]{0,1})0{0,1}){0,1}[2|3|7|8]{1}){0,1}[ -]*(d{4}[ -]{0,1}d{4}))|(1[ -]{0,1}(300|800|900|902)[ -]{0,1}((d{6})|(d{3}[ -]{0,1}d{3})))|(13[ -]{0,1}([d -]{5})|((({0,1}[ -]{0,1})0{0,1}){0,1}4{1}[d -]{8,10})))$
所以我得到html页http://www.boat-duesseldorf.com/cgi-bin/md_boot/lib/pub/pub/pub/pub/pub/tt.cgi一下Nokorigi和Make:
/^(+d{2}[ -]{0,1}){0,1}(((({0,1}[ -]{0,1})0{0,1}){0,1}[2|3|7|8]{1}){0,1}[ -]*(d{4}[ -]{0,1}d{4}))|(1[ -]{0,1}(300|800|900|902)[ -]{0,1}((d{6})|(d{3}[ -]{0,1}d{3})))|(13[ -]{0,1}([d -]{5})|((({0,1}[ -]{0,1})0{0,1}){0,1}4{1}[d -]{8,10})))$/.match.(@html)
和 pp Regexp.last_match
-但获得零!
我需要良好的Regexp,请推荐100%工作REGEXP或模块。
我对Ruby不熟悉,但希望它会有所帮助:
require 'net/http'
require 'uri'
site = 'http://www.boat-duesseldorf.com/cgi-bin/md_boot/lib/pub/tt.cgi/Overview_Press.html?oid=58288&lang=2&ticket=g_u_e_s_t'
html = Net::HTTP.get(URI.parse(site))
phones = html.scan(/[^0-9](+[0-9()- ]{2,})/)
puts phones
输出:
+49 (0)211 4560-518
+49 (0)211 4560-87-518
+49 (0)211 4560-589
+49 (0)211 4560-87-589
+49 (0)211 4560-990
+49 (0)211 4560-545