C# 获取网页上指定的元素(.net Framework4.0 兼容版本)
using System;
using System.IO;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using HtmlAgilityPack;
using Newtonsoft.Json;
class Program
{
static void Main(string[] args)
{
var url = "https://go-upc.com/search?q=776545980144"; // 替换为你要获取数据的网页地址
var htmlContent = DownloadHtml(url);
var htmlDocument = new HtmlDocument();
htmlDocument.LoadHtml(htmlContent);
var productNameNode = htmlDocument.DocumentNode.SelectSingleNode("//h1[@class='product-name']");
var eanNode = htmlDocument.DocumentNode.SelectSingleNode("//td[@class='metadata-label'][contains(text(), 'EAN')]/following-sibling::td");
var upcNode = htmlDocument.DocumentNode.SelectSingleNode("//td[@class='metadata-label'][contains(text(), 'UPC')]/following-sibling::td");
var descriptionNode = htmlDocument.DocumentNode.SelectSingleNode("//h2[contains(text(), 'Description')]/following-sibling::span");
if (productNameNode != null && eanNode != null && upcNode != null && descriptionNode != null)
{
var productName = productNameNode.InnerText.Trim();
var ean = eanNode.InnerText.Trim();
var upc = upcNode.InnerText.Trim();
var description = descriptionNode.InnerText.Trim();
var jsonData = new
{
ProductName = productName,
EAN = ean,
UPC = upc,
Description = description
};
var jsonResult = JsonConvert.SerializeObject(jsonData);
Console.WriteLine(jsonResult); // 返回JSON数据给前端
Console.ReadLine();
}
}
private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
{
return true;
}
static string DownloadHtml(string url)
{
string htmlContent = null;
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
// 重點是修改這行
ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072; // SecurityProtocolType.Tls1.2
using (WebClient client = new WebClient())
{
client.Encoding = Encoding.UTF8;
htmlContent = client.DownloadString(url);
}
return htmlContent;
}
}