火车头运行c#代码获取header头部的Location跳转值
因为有些下载站下载采用跳转,所以你采集要根据header进行获取它的跳转结果值,所以就写这个插件
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using SpiderInterface;
class LocoyCode
{
/// <summary>
/// 执行方法,不能修改类和方法名称。
/// </summary>
/// <param name="content">标签内容</param>
/// <param name="response">页面响应,包含了Url、原始Html等属性</param>
/// <returns>返回JSON中的location字段值</returns>
public string Run(string content, ResponseEntry response)
{
// 空值判断
if (response == null || string.IsNullOrEmpty(response.RawText))
{
return "";
}
// 正则匹配JSON中的location字段(适配你当前的格式)
// 匹配规则:"location":[{"locatio 前面的有效地址
Match m = Regex.Match(response.RawText, @"([^"",]+)""\s*,\s*""location""\s*:\s*\[\{""locatio", RegexOptions.IgnoreCase);
// 如果匹配成功返回地址,否则尝试匹配其他JSON location格式
if (m.Success)
{
return m.Groups[1].Value.Trim();
}
// 备用匹配:直接匹配 "location":"xxx" 格式
Match m2 = Regex.Match(response.RawText, @"""location""\s*:\s*""(?<url>[^""]+)""", RegexOptions.IgnoreCase);
if (m2.Success)
{
return m2.Groups["url"].Value.Trim();
}
// 备用匹配:匹配 "location":[{"location":"xxx"}] 格式
Match m3 = Regex.Match(response.RawText, @"""location""\s*:\s*\[\{\s*""location""\s*:\s*""(?<url>[^""]+)""", RegexOptions.IgnoreCase);
if (m3.Success)
{
return m3.Groups["url"].Value.Trim();
}
return "";
}
}
在数据处理这里运行即可

千行代码,Bug何处藏。 纵使上线又怎样,朝令改,夕断肠。

浙公网安备 33010602011771号