Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sockets 使用luasocket和代理获取url页面_Sockets_Lua_Fetch_Luasocket - Fatal编程技术网

Sockets 使用luasocket和代理获取url页面

Sockets 使用luasocket和代理获取url页面,sockets,lua,fetch,luasocket,Sockets,Lua,Fetch,Luasocket,到目前为止,我有以下作品: local socket = require "socket.http" client,r,c,h = socket.request{url = "http://example.com/", proxy="<my proxy and port here>"} for i,v in pairs( c ) do print( i, v ) end 这意味着连接建立得恰到好处。现在,我想使用这个s

到目前为止,我有以下作品:

local socket = require "socket.http"
client,r,c,h = socket.request{url = "http://example.com/", proxy="<my proxy and port here>"}
for i,v in pairs( c ) do
  print( i, v )
end
这意味着连接建立得恰到好处。现在,我想使用这个
socket.http
获取我的
url的标题。我搜索了之前的SO问题和答案。但是,我仍然不知道如何在变量中获取/存储整个/部分页面,并对其进行处理


请提供帮助。

您使用的是http.request()的“通用”形式,它要求通过LTN12接收器存储正文。它不像听起来那么复杂,请尝试以下代码:

local socket = require "socket.http"
local ltn12 = require "ltn12"; -- LTN12 lib provided by LuaSocket

-- This table will store the body (possibly in multiple chunks):
local result_table = {};
client,r,c,h = socket.request{
    url = "http://example.com/",
    sink = ltn12.sink.table(result_table),
    proxy="<my proxy and port here>"
}
-- Join the chunks together into a string:
local result = table.concat(result_table);
-- Hacky solution to extract the title:
local title = result:match("<[Tt][Ii][Tt][Ll][Ee]>([^<]*)<");
print(title);

谢谢这通常适用于所有类型的页面。:)但是,在尝试获取youtube链接的标题时,
result
变量中只有页面。我两种方法都试过了。第二个可以更快地获取页面。:)我刚刚更新了一个示例YouTube链接和我得到的输出。对我来说一切都很好。标题中有空格填充,有时可能还有HTML实体。你可能会想通过剥离和转换它们来规范化它。不,还没起作用。我正在SciTe中运行文件(名为
02.lua
)。下面是输出和代码的屏幕截图(我使用了4个不同的网页,2个在我自己的web服务器上)。检查:有趣。我只能猜测这与您的代理有关,因为这是您的代码和我的代码之间的唯一区别。要调试类似的东西,我通常会接触Wireshark,并记录请求和响应,以查看是否有任何意外情况。你是说它对一些页面有效,但不是所有页面都有效吗?这里是另一个截图,其中包含一些网站的示例。正如你所看到的,
youtube.com
被重定向到谷歌,
stackoverflow
甚至没有被打开,除此之外,luasocket的官方页面返回404错误。
local socket = require "socket.http"
local ltn12 = require "ltn12"; -- LTN12 lib provided by LuaSocket

-- This table will store the body (possibly in multiple chunks):
local result_table = {};
client,r,c,h = socket.request{
    url = "http://example.com/",
    sink = ltn12.sink.table(result_table),
    proxy="<my proxy and port here>"
}
-- Join the chunks together into a string:
local result = table.concat(result_table);
-- Hacky solution to extract the title:
local title = result:match("<[Tt][Ii][Tt][Ll][Ee]>([^<]*)<");
print(title);
local http = require "socket.http"
http.PROXY="<my proxy and port here>"

local result = http.request("http://www.youtube.com/watch?v=_eT40eV7OiI")
local title = result:match("<[Tt][Ii][Tt][Ll][Ee]>([^<]*)<");
print(title);
    Flanders and Swann - A song of the weather
  - YouTube