1 require 'open-uri'
2 require 'net/http'
3 require 'iconv'
4 require 'uri'
5
6 src1_file = "place_file.txt"
7 src2_file = "things_file.txt"
8 save_file = open("key_word_rank_baidu.txt","w")
9 max_num = '100'
10
11 File.open(src1_file).readlines.each do |place|
12 File.open(src2_file).readlines.each do |thing|
13 enc_pl = URI.escape(Iconv.conv('gbk','utf-8',place))
14 enc_th = URI.escape(Iconv.conv('gbk','utf-8',thing))
15 query = enc_pl.to_s.chomp+"+"+enc_th.to_s.chomp
16 url='http://www.baidu.com/s?wd='+query+'&rn='+max_num
17 target=open(url).read.scan(/<font\scolor=#008000>.*?<\/font>/)
18 puts target
19 rank = 0
20 if target != nil
21 for val in target
22 if (val =~ /<font\scolor=#008000>.*?\s[0-9]+K\s.*?<\/font>/) != nil
23 rank += 1
24 if (val =~ /xxxxx/) != nil
25 save_file.puts val
26 save_file.puts query
27 save_file.puts rank.to_s
28 break
29 end
30 end
31 end
32 end
33 end
34 end
35
36 email_to = "xxx@gmail.com"
37 file = "key_word_rank.txt"
38
39 sendmail_cmd = "cat #{file} | mailx -s daodaorank #{email_to}"
40 pipe = IO.popen(sendmail_cmd)
2 require 'net/http'
3 require 'iconv'
4 require 'uri'
5
6 src1_file = "place_file.txt"
7 src2_file = "things_file.txt"
8 save_file = open("key_word_rank_baidu.txt","w")
9 max_num = '100'
10
11 File.open(src1_file).readlines.each do |place|
12 File.open(src2_file).readlines.each do |thing|
13 enc_pl = URI.escape(Iconv.conv('gbk','utf-8',place))
14 enc_th = URI.escape(Iconv.conv('gbk','utf-8',thing))
15 query = enc_pl.to_s.chomp+"+"+enc_th.to_s.chomp
16 url='http://www.baidu.com/s?wd='+query+'&rn='+max_num
17 target=open(url).read.scan(/<font\scolor=#008000>.*?<\/font>/)
18 puts target
19 rank = 0
20 if target != nil
21 for val in target
22 if (val =~ /<font\scolor=#008000>.*?\s[0-9]+K\s.*?<\/font>/) != nil
23 rank += 1
24 if (val =~ /xxxxx/) != nil
25 save_file.puts val
26 save_file.puts query
27 save_file.puts rank.to_s
28 break
29 end
30 end
31 end
32 end
33 end
34 end
35
36 email_to = "xxx@gmail.com"
37 file = "key_word_rank.txt"
38
39 sendmail_cmd = "cat #{file} | mailx -s daodaorank #{email_to}"
40 pipe = IO.popen(sendmail_cmd)
本文介绍了一款使用 Ruby 编写的爬虫程序,该程序能够抓取百度搜索引擎中特定关键词的排名情况。通过读取地方名称和事物名称的文件组合成查询字符串,然后请求百度搜索页面并解析返回结果,提取出排名信息。最终将结果保存到文件,并通过邮件发送报告。
720

被折叠的 条评论
为什么被折叠?



