爬虫实现(hpricot)

1.基本代码

 在gemfile中加入gem "hpricot",bundler install之后,在application。rb中require "hpricot" require "open-uri". 

 

复制代码
 1 pp "===========begin============="
 2 url = "http://www.xiaochuncnjp.com/search.php?mod=forum&searchid=552&orderby=lastpost&ascdesc=desc&searchsubmit=yes&kw=%E6%90%AC%E5%AE%B6"
 3 doc = Hpricot(open(url))
 4 # 获取返回页面的编码,使用了gem rchardet。
 5 cd = CharDet.detect(doc.to_s)
 6 pp encoding = cd["encoding"]
 8 # pp doc.search("ul/.pbw")  #获取返回页面ul标签下class为pbw的元素
 9 doc.search("ul/.pbw").each do |item|
10   # pp timeStr = item.inner_html
11   pp titleStr = item.search("h3/a").inner_html
12   pp urlStr = item.search("h3").inner_html.to_s.gsub(/href="/, 'href="http://www.xiaochuncnjp.com/')
13   pp contentStr = item.search("p")[1].inner_html     
14 end
15 pp "************end***********"

复制代码

 

2。当链接的协议为https时,报certificate verify failed error,无法通过认证的错误。

  https是安全协议,要通过验证可以add this ssl_verify option to the top of the file.来解决

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
FROM:
 
 module OpenURI
  Options = {
    :proxy => true,
    :progress_proc => true,
    :content_length_proc => true,
    :http_basic_authentication => true,
  }
 
 TO:
 
 module OpenURI
  Options = {
    :proxy => true,
    :progress_proc => true,
    :content_length_proc => true,
    :http_basic_authentication => true,
    :ssl_verify => true
  }
 
 Change the part where it enables verification
 
 FROM:
 
    if target.class == URI::HTTPS
      require 'net/https'
      http.use_ssl = true
      http.enable_post_connection_check = true
      http.verify_mode = OpenSSL::SSL::VERIFY_PEER
      store = OpenSSL::X509::Store.new
      store.set_default_paths
      http.cert_store = store
    end
 
 TO:
    if target.class == URI::HTTPS
      require 'net/https'
      http.use_ssl = true
      http.enable_post_connection_check = true
      if options[:ssl_verify] == false
        http.verify_mode = OpenSSL::SSL::VERIFY_NONE
      else
        http.verify_mode = OpenSSL::SSL::VERIFY_PEER
      end
      store = OpenSSL::X509::Store.new
      store.set_default_paths
      http.cert_store = store
    end
 
 run it like this:
 
 open("https://someurl", :ssl_verify => false) {|f|
  print f.read
 }

3.页面乱码

   由于网页的编码方式不同意,当你摘录信息的时候,很容易出现乱码。因此,你需要根据网页的编码方式转换编码。这个过程使用到了rchardet插件。

4.rchardet的使用

  在gemfile中加入gem "rchardet",bundler install之后,在application。rb中require "rchardet".

 

1
2
3
4
cd = CharDet.detect(some_data)
  encoding = cd['encoding']
  confidence = cd['confidence'] # 0.0 <= confidence <= 1.0
  eg: CharDet.detect("\xA4\xCF"#=>  {"encoding"=>"EUC-JP", "confidence"=>0.99}

  

posted @   鞋带松了  阅读(310)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话
点击右上角即可分享
微信分享提示