【问题标题】:C# Fetching the HTML Code of a website does not workC# 获取网站的 HTML 代码不起作用
【发布时间】:2015-02-07 14:38:15
【问题描述】:

我一直在尝试下载某个网站的html源代码,从这个网址更精确:

http://steamcommunity.com/market/search?q=&category_730_ItemSet[]=any&category_730_TournamentTeam[]=any&category_730_Weapon[]=any&category_730_Quality[]=tag_normal&appid=730#p10_price_desc

当我将此 URL 放入我的网络浏览器 (Firefox) 时,它会将我带到 Steam 市场的第 10 页,因为在 URL 的末尾,它显示“p10”。当我用 12 替换 10 时,它会将我带到第 12 页。但是,当我尝试在 C# 中使用此代码下载第 10 页时,

WebClient webClient = new WebClient();
string pageURL = downloadURL + getURLEnding(currentPage);

string html = webClient.DownloadString(pageURL);
var doc = new HtmlDocument();
doc.LoadHtml(html);

它似乎忽略了网址的那一点。无论我用什么数字替换 10,我总是得到相同的结果。为什么?

【问题讨论】:

    标签: c# html webclient


    【解决方案1】:

    您会得到相同的结果,因为 Steam 的页面实际上是使用 AJAX 获取结果,这意味着您想要的内容不在您获得的页面中。

    这是您在第 10 页时浏览器获取的 URL(结果在 JSON format 中):

    http://steamcommunity.com/market/search/render/?query=&start=90&count=10&search_descriptions=0&sort_column=price&sort_dir=desc&appid=730&category_730_ItemSet%5B%5D=any&category_730_TournamentTeam%5B%5D=any&category_730_Weapon%5B%5D=any&category_730_Quality%5B%5D=tag_normal
    

    注意此 URL 中的start 参数,该参数是根据页码计算得出的。

    【讨论】:

    • 有什么简单的方法可以解决这个问题吗?我的意思是让页面先获取结果然后下载源代码?
    • @DealerJoe:是的。只需直接从我的答案中引用的 URL 获取结果。但这意味着解析 JSON 而不是 HTML。
    猜你喜欢
    • 1970-01-01
    • 2019-08-12
    • 2014-08-16
    • 2017-01-26
    • 1970-01-01
    • 2013-12-09
    • 1970-01-01
    • 2012-01-30
    • 1970-01-01
    相关资源
    最近更新 更多