【问题标题】:How to read someone else's forum如何阅读别人的论坛
【发布时间】:2011-01-04 20:07:48
【问题描述】:

我的朋友有一个论坛,里面到处都是包含信息的帖子。有时她想查看论坛中的帖子并得出结论。目前,她通过点击她的论坛来查看帖子,并生成不一定准确的数据图片(在她的大脑中),她从中得出结论。我今天的想法是,我可能会编写一个快速的 Ruby 脚本来解析必要的 HTML,让她真正了解数据在说什么。

我今天第一次使用Ruby的net/http库,遇到了一个问题。虽然我的浏览器在查看我朋友的论坛时没有问题,但 Net::HTTP.new("forumname.net") 方法似乎产生了以下错误:

由于目标机器主动拒绝,无法建立连接。 - 连接(2)

通过谷歌搜索该错误,我了解到它与 MySQL(或类似的东西)有关,不希望像我这样爱管闲事的人远程在那里闲逛:出于安全原因。这对我来说是有道理的,但它让我想知道:我的浏览器怎么会在我朋友的论坛上闲逛,但我的小 Ruby 脚本却没有任何权限。我的脚本有什么方法可以告诉服务器它不是威胁吗?我只想要阅读权而不是写作权?

谢谢大家,

z.

【问题讨论】:

  • 您是想直接访问她的数据库还是只访问 HTML 页面?您似乎对端口的工作方式有些困惑:“我了解到它与 MySQL 有关”
  • 我是!也许我的问题与“代理”有关?我尝试调用 address = Net::HTTP.new("sitename.net") 并且对于某些站点,我会得到 HTML,而对于某些站点,我会收到该错误消息。
  • 更改了您问题的标签,因为它似乎真的是为了让 httpwebrequest 正常工作

标签: ruby httpwebrequest mechanize screen-scraping


【解决方案1】:

抓取网站?使用mechanize:

#!/usr/bin/ruby1.8

require 'rubygems'
require 'mechanize'

agent = WWW::Mechanize.new
page = agent.get("http://xkcd.com")
page = page.link_with(:text=>'Forums').click
page = page.link_with(:text=>'Mathematics').click
page = page.link_with(:text=>'Math Books').click
#puts page.parser.to_html    # If you want to see the html you just got
posts = page.parser.xpath("//div[@class='postbody']")
for post in posts
  title = post.at_xpath('h3//text()').to_s
  author = post.at_xpath("p[@class='author']//a//text()").to_s
  body = post.xpath("div[@class='content']//text()").collect do |div|
    div.to_s
  end.join("\n")
  puts '-' * 40
  puts "title: #{title}"
  puts "author: #{author}"
  puts "body:", body
end

输出的第一部分:

----------------------------------------
title: Math Books
author: Cleverbeans
body:
This is now the official thread for questions about math books at any level, fr\
om high school through advanced college courses.
I'm looking for a good vector calculus text to brush up on what I've forgotten.\
 We used Stewart's Multivariable Calculus as a baseline but I was unable to pur\
chase the text for financial reasons at the time. I figured some things may hav\
e changed in the last 12 years, so if anyone can suggest some good texts on thi\
s subject I'd appreciate it.
----------------------------------------
title: Re: Multivariable Calculus Text?
author: ThomasS
body:
The textbooks go up in price and new pretty pictures appear. However, Calculus \
really hasn't changed all that much.
If you don't mind a certain lack of pretty pictures, you might try something li\
ke Widder's Advanced Calculus from Dover. it is much easier to carry around tha\
n Stewart. It is also written in a style that a mathematician might consider no\
rmal. If you think that you might want to move on to real math at some point, i\
t might serve as an introduction to the associated style of writing.

【讨论】:

  • oooooh,也许这就是我感兴趣的东西。机械化,你说,Ruby gems 你说:我充满了好奇心!我走了……去谷歌!
  • 不只是我,其他三个人都这么说。这是道义上的要求!您会发现 mechanize 的 rdoc 页面有点多余,而且 xpath 起初(和第二次)可能令人生畏,但您会因为学习如何使用它们而付出的努力得到很好的回报。以这种方式抓取网络的速度更快。对于 SO 工厂,您有任何问题。
【解决方案2】:

某些网站只能使用“www”子域访问,因此可能会导致问题。

要创建一个 get 请求,您需要使用 Get 方法:

require 'net/http'

url = URI.parse('http://www.forum.site/')
req = Net::HTTP::Get.new(url.path)
res = Net::HTTP.start(url.host, url.port) {|http|
  http.request(req)
}
puts res.body

你可能还需要在某个时候设置用户代理作为一个选项:

{'User-Agent' => 'Mozilla/5.0 (Windows; U;
    Windows NT 5.1; en-US; rv:1.8.0.1) Gecko/20060111 Firefox/1.5.0.1'})

【讨论】:

  • 是的,用户代理也是我的猜测。
  • 这就是问题所在。当我使用例如 url = URI.parse('forums.xkcd.com') 运行此类代码时,我收到有关“目标机器主动拒绝它”的错误消息。这是我要克服的问题。
  • @Ziggy,网址为 http://forums.xkcd.com/(斜杠和协议很重要)它应该可以工作
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-18
  • 2013-06-19
  • 2011-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多