这段代码据说可以获取任何网页的内容:
require 'socket'
host = 'www.tutorialspoint.com' # The web server
port = 80 # Default HTTP port
path = "/index.htm" # The file we want
# This is the HTTP request we send to fetch a file
request = "GET #{path} HTTP/1.0\r\n\r\n"
socket = TCPSocket.open(host,port) # Connect to server
socket.print(request) # Send request
response = socket.read # Read complete response
# Split response at first blank line into headers and body
headers,body = response.split("\r\n\r\n", 2)
puts headers
puts body
当我在命令行中运行它时,我得到一个404错误,但是当我去www.tutorialspoint.com/index.htm时它就在那里,那么它给出了什么?:
虽然我在使用open-uri库来获取网页内容时遇到了麻烦。但我想知道如何使用这个。
答案 0 :(得分:1)
您的请求错过了Host参数:
Table : people
Name(Column, VARCHAR) - "John"
Height(Column, INT) - 165
请注意,显然并非所有网络服务器都需要"主机:"线(但见评论)。