c# net爬取百度热搜

var url = "https://top.baidu.com/board?tab=realtime";//百度
using (var httpClient = new HttpClient())
{

var response = await httpClient.GetStringAsync(url);

var htmlDocument = new HtmlDocument();
htmlDocument.LoadHtml(response);
var categoryWrapNode = htmlDocument.DocumentNode.SelectNodes("//div[@class='category-wrap_iQLoo horizontal_1eKyQ']");

if (categoryWrapNode != null)
{
foreach (var div in categoryWrapNode)
{
var divA = div.SelectSingleNode(".//div[@class='c-single-text-ellipsis']");
var divB = div.SelectSingleNode(".//div[@class='hot-desc_1m_jR large_nSuFU ']");
var divC = div.SelectSingleNode(".//div[@class='hot-index_1Bl1a']");
var anchorTag = div.SelectSingleNode(".//div[@class='hot-desc_1m_jR large_nSuFU ']/a");
if (divB == null)
{
divB = div.SelectSingleNode(".//div[@class='hot-desc_1m_jR small_Uvkd3 ellipsis_DupbZ']");
}

var hrefValue = "";
if (anchorTag != null)
{
// 提取 href 属性
hrefValue = anchorTag.GetAttributeValue("href", string.Empty);
}
divB.RemoveChild(anchorTag);
string atext = divA.InnerText.Trim();
string btext = divB.InnerText.Trim();
string ctext = divC.InnerText.Trim();

}

根据这个方法制作了这个合集

http://www.cooool.live/

 

posted @ 2024-08-15 14:29  過朢  阅读(51)  评论(0)    收藏  举报