gpt4 book ai didi

c# - 如何使用 HtmlAgilityPack 解析

转载 作者:行者123 更新时间:2023-11-30 13:17:12 24 4
gpt4 key购买 nike

上下文:

我正在尝试从这个 Page here 中解析“城市” .我已经设法模拟了对这个组合框数据的请求,这是一个 Ajax 调用。

fiddler 请求:

POST http://www.telelistas.net/AjaxHandler.ashx HTTP/1.1
Host: www.telelistas.net
Connection: keep-alive
Content-Length: 106
Origin: http://www.telelistas.net
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.97 Safari/537.11
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Accept: */*
Referer: http://www.telelistas.net/
Accept-Encoding: gzip,deflate,sdch
Accept-Language: pt-BR,pt;q=0.8,en-US;q=0.6,en;q=0.4
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
Cookie: cert_Origin=directo; email=bdc.testes@gmail.com; auto=automatico=0; searchparameters=bottom=0&btnsite=0&email=&uf=rj&origem=0&nome=&pagina=1&codlogradouro=&predio=213&tiquete=0&localidadeendmap=&codbairro=0&pcount=25&estacionamento=0&letra=&top=&entrega=0&pchave=&info=&logradouro=rua+da+lapa&codtitulo=-1&chave=&zoom=&comercial=0&ddd=0&comib=0&btnemail=0&pgresultado=&localidade=&telefone=&manobrista=0&codlocalidade=21000&site=&cartoes=0&atividade=&bairro=&reserva=0&residencial=0; perfil=logged=1&iduser=2563063&email=bdc.testes@gmail.com&usertype=2&specialsearch=3&siteusernome=BigDataCorp&siteuserdatanasc=15/01/1988&siteusersexo=M&siteuserlocalidade=21000&siteuseruf=RJ&siteuserddd=21&siteusertelefone=94118439&siteuserprofissao=4&siteuserrenda=5000&siteuserformacao=4&siteusernovidades=0&siteusernovidadesrevista=&siteusernovidadesparceiros=0&siteusercpf=10541308769&siteuseracesso=brasil&siteusercep=22631000&siteuseridade=24&siteuserparceiro=telelistas&siteuserconhecimento=2&siteuseroperadora=oi&siteuserurlorigem=http://www.telelistas.net/&siteuserdatacadastro=13/12/2012 11:45:00; __utma=70879631.392027796.1355939587.1356014801.1356021821.5; __utmb=70879631.1.10.1356021821; __utmc=70879631; __utmz=70879631.1355939587.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)

PostData : state=rj&style=busca_interna&selectedCity=21000&clientId=pch_localidade_select&method=GetSearchCitiesNamed

问题:

这是此请求返回的字符串的片段:

<select name='pch_localidade_select' class='busca_interna' id='pch_localidade_select' tabindex="4"><option value="">Selecione</option><option selected value="21000">Rio de Janeiro</option><option value="21380">Abraão</option><option value="21001">Afonso Arinos</option><option value="21002">Agência Luterback</option><option value="21847">Agriões de Dentro</option>

我想做的是到达选项标签(“Rio de Janeiro”、“Abraao”...)的 InnerText,但由于某些奇怪的原因,InnerText 对于找到的每个选项节点始终为空。

有一些代码片段失败了:

        // Iterating over nodes to build the dictionary
foreach (HtmlNode city in citiesNodes)
{
string key = city.InnerText;
string value = city.Attributes["value"].Value;

citiesHash.AddCity (key,value);
}

现有技术:

我正在使用 HtmlAgilityPack支持 XPath节点选择语法、C# 代码和 Fiddler2用于 Web 调试。

提前致谢

最佳答案

只需在加载 html 之前使用 HtmlAgilityPack.HtmlNode.ElementsFlags.Remove("option");

HtmlAgilityPack.HtmlNode.ElementsFlags.Remove("option");

HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(html);

var options = doc.DocumentNode.Descendants("option").Skip(1)
.Select(n => new
{
Value = n.Attributes["value"].Value,
Text = n.InnerText
})
.ToList();

关于c# - 如何使用 HtmlAgilityPack 解析 <option> 标签的 InnerText?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13977243/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com