html - Getting attribute of element in XPath -
i want learn web-scraping. therefore, started practicing. trying data-ad-id html
using xpath
.
html
structure this:
<body id="z1234"> <div class="viewport"> <div class="g-row"> <div class="g-col-9"> <div class="cbox cbox--content cbox--resultlist"> <div class="cbox-body cbox-body--resultitem dealerad rbt-reg rbt-no-top"><a class="link--muted no--text--decoration result-item" href="url" data-ad-id="248059713"></a> </div> </div> </div> </div> </body>
xpath <a class="link--muted no--text--decoration result item" >
//*[@id="z1234"]/div[3]/div[4]/div[2]/div[1]/div[11]/a
. if choose different car, last div changes.
according write c#
code:
var url = "https://suchen.mobile.de/fahrzeuge/search.html?damageunrepaired=no_damage_unrepaired&issearchrequest=true&maxpowerasarray=kw&maxprice=10000&minpowerasarray=kw&minprice=10000&scopeid=c"; httpwebrequest request = (httpwebrequest)webrequest.create(url); httpwebresponse response = (httpwebresponse)request.getresponse(); streamreader sr = new streamreader(response.getresponsestream()); string sourcecode = sr.readtoend(); htmlagilitypack.htmldocument document = new htmlagilitypack.htmldocument(); document.loadhtml(sourcecode); var rows = document.documentnode.selectnodes("//*[@id='z1234']/div[3]/div[4]/div[2]/div[1]/div[11]"); foreach (var row in rows) { var id = row.selectsinglenode("a[@data-ad-id]").innertext; console.writeline("id:" + id); } }
i cannot node. null. how can data-ad-id?
edit change c# code:
var rows = document.documentnode.selectnodes("//a[@data-ad-id]")[0]; var id = rows.attributes["data-ad-id"].value;
now can data-ad-id.
as per code of site, sense have no innertext "a" tag. contains div , img tags.
you need fetch data-ad-id using
//a[@data-ad-id]/@data-ad-id
Comments
Post a Comment