c# - How to separate this code in two classes? -
i want code html scraper in c# can links or other targetted strings page want to.
i began , ran straight problem: have no idea how seperate code in classes, can use different search engines.
this current code:
using system; using system.collections.generic; using system.linq; using system.text; using system.threading.tasks; using system.diagnostics; using system.net; using htmlagilitypack; namespace scraper.components { class scraper { public scraper() { webclient client = new webclient(); client.headers.add("user-agent", "mozilla/4.0 (compatible; msie 7.0; windows nt 5.1; .net clr 2.0.50727; .net clr 1.1.4322; .net clr 3.0.04506.30; .net clr 3.0.04506.648)"); htmldocument doc = new htmldocument(); doc.load(client.openread("xxx")); htmlnode rootnode = doc.documentnode; htmlnodecollection adnodes = rootnode.selectnodes("//a[@class='ad-title']"); foreach(htmlnode adnode in adnodes) { debug.writeline( adnode.attributes["href"].value ); } } } }
my intention seperate whole code below client.headers.add
independent class, call example:
scraper scraper = new scraper(new googlese('http://google.com/...'));
or that.
thanks in advance.
just starter idea far, should work.
this beginning of the strategy pattern. may want @ factory pattern generating search engine objects.
namespace scraper.components { class scraper { public scraper(isearchengine engine) { webclient client = new webclient(); client.headers.add("user-agent", "mozilla/4.0 (compatible; msie 7.0; windows nt 5.1; .net clr 2.0.50727; .net clr 1.1.4322; .net clr 3.0.04506.30; .net clr 3.0.04506.648)"); engine.search(client); } } class googlese: isearchengine { public void search(webclient client){ htmldocument doc = new htmldocument(); doc.load(client.openread("http:\\google.com")); htmlnode rootnode = doc.documentnode; htmlnodecollection adnodes = rootnode.selectnodes("//a[@class='ad-title']"); foreach(htmlnode adnode in adnodes) { debug.writeline( adnode.attributes["href"].value ); } } class bingse: isearchengine { public void search(webclient client){ htmldocument doc = new htmldocument(); doc.load(client.openread("http:\\bing.com")); htmlnode rootnode = doc.documentnode; htmlnodecollection adnodes = rootnode.selectnodes("//a[@class='ad-title']"); foreach(htmlnode adnode in adnodes) { debug.writeline( adnode.attributes["href"].value ); } } } interface isearchengine{ void search(); } }
Comments
Post a Comment