sách gpt4 ai đã đi

sockets - 使用 luasocket 和代理获取 url 页面

In lại 作者:行者123 更新时间:2023-12-04 20:17:33 27 4
mua khóa gpt4 Nike

到目前为止,我有以下部分:

local socket = require "socket.http"
client,r,c,h = socket.request{url = "http://example.com/", proxy=""}
for i,v in pairs( c ) do
print( i, v )
kết thúc
这给了我如下输出:
connection close
content-type text/html; charset=UTF-8
location http://www.iana.org/domains/example/
vary Accept-Encoding
date Tue, 24 Apr 2012 21:43:19 GMT
last-modified Wed, 09 Feb 2011 17:13:15 GMT
transfer-encoding chunked
server Apache/2.2.3 (CentOS)
这意味着刚刚建立的连接 完美 .现在,我想获取我的 url's 的标题使用这个 socket.http .我搜索了以前的 SO 问题和 luasocket's http documentation .但是,我仍然不知道如何在变量中获取/存储页面的整个/部分并对其进行处理。
请帮忙。

1 Câu trả lời

您正在使用 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=""
}
-- 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);

如果您的代理在整个应用程序中保持不变,那么更直接的解决方案是使用 http.request() 的简单形式,并通过 http.PROXY 指定代理:
local http = require "socket.http"
http.PROXY=""

local result = http.request("http://www.youtube.com/watch?v=_eT40eV7OiI")
local title = result:match("<[Tt][Ii][Tt][Ll][Ee]>([^<]*)<");
print(title);

Đầu ra:
    Flanders and Swann - A song of the weather
- YouTube

关于sockets - 使用 luasocket 和代理获取 url 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10306489/

27 4 0
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com