-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCLScraper.cs
More file actions
137 lines (120 loc) · 5.48 KB
/
Copy pathCLScraper.cs
File metadata and controls
137 lines (120 loc) · 5.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
using System;
using System.Globalization;
using System.Xml;
using HtmlAgilityPack;
using System.Linq;
namespace CLReader
{
static public class CLScraper
{
static public void Scrape(SearchTerm st,string cityList,string clDomain,Matches matches,Matches lastMatches)
{
//If there are multiple terms, loop through them
foreach(string searchTermToUse in st.CLSearch.Split(',').ToList())
{
foreach(string city in cityList.Split(',').ToList())
{
var url = $"https://{city}.craigslist{clDomain}/search/boo?query={searchTermToUse}&min_price={st.MinPrice}&max_price={st.MaxPrice}&srchType=T";
CLScrape(url,st,matches,lastMatches,searchTermToUse);
}
}
}
static private void CLScrape(string url,SearchTerm st,Matches matches,Matches lastMatches,string searchTermToUse)
{
HtmlWeb web = new HtmlWeb();
var _doc = web.Load(url);
var findclasses = _doc.DocumentNode
.Descendants()
.Where( d =>
d.Attributes.Contains("class")
&&
(d.Attributes["class"].Value.Contains("result-row"))
);
foreach(var node in findclasses)
{
try
{
//Get title and listing link
var title = node
.Descendants()
.Where( d =>
d.Attributes.Contains("class")
&&
d.Attributes["class"].Value.Contains("result-title")
).Single();
//Get title and link
string link=title.Attributes["href"].Value;
string titleString= System.Net.WebUtility.HtmlDecode(title.InnerText);
//Get location
var meta = node
.Descendants()
.Where( d =>
d.Attributes.Contains("class")
&&
d.Attributes["class"].Value.Contains("result-meta")
).FirstOrDefault();
//Parse location
string locationString="(unknown)";
string rawLoc=meta.InnerText;
int start=rawLoc.IndexOf('(');
if(start>=0)
{
int end=rawLoc.IndexOf(')');
locationString=System.Net.WebUtility.HtmlDecode(rawLoc.Substring(start,(end-start)+1));
}
//Get listing date
var listing = node
.Descendants()
.Where( d =>
d.Attributes.Contains("class")
&&
d.Attributes["class"].Value.Contains("result-date")
).Single();
//Parse date
string listingDateStr = listing.Attributes["datetime"].Value;
DateTime publishDate = DateTime.Parse(listingDateStr);
//Get Price
var price = node
.Descendants()
.Where( d =>
d.Attributes.Contains("class")
&&
d.Attributes["class"].Value.Contains("result-price")
).FirstOrDefault();
//Parse currency
string priceString=price.InnerText;
start=priceString.IndexOf('$');
string rawPrice=priceString.Substring(start);
decimal decPrice = decimal.Parse(rawPrice, NumberStyles.Any);
//Get and check price (zero if can't parse)
bool priceFlag = false;
if((((long)decPrice == 0 && !st.IgnoreZeroPrice) || (long)decPrice >= st.MinPrice) && (long)decPrice <= st.MaxPrice)
priceFlag=true;
//Check for exclusions
bool excludeKeywordsFlag = Helper.CheckTitle(st.ExcludeKeywords,titleString);
bool excludeCharsFlag = Helper.CheckTitle(st.ExcludeChars,titleString);
//If we're still good to go, let's add the item
if(excludeKeywordsFlag && excludeCharsFlag && priceFlag)
{
//Add
Item item = new Item
{
Link=link,
Title=titleString + " " + locationString + " " + priceString,
SearchString=searchTermToUse,
WebSite="CList",
Starred=st.Starred,
PublishDate=publishDate
};
matches.AddItem(item,lastMatches);
}
}
catch (System.Exception e)
{
Console.WriteLine(e.Message);
//Console.WriteLine($"Problem parsing: {node.OuterHtml}");
}
}
}
}
}