Inside my app I would like to scrape the price of any product (user types in the wanted URL).
I searched quite a bit now and I found out that there are couple of Webscrapers
, I think I will use SwiftSoup
for now. However I couldn't find a single tutorial that teaches how to scrape
for elements with "dynamic" tags
. For example the price
of a product on a website looks different for every website:
Example 1:
<div class="price">82 EUR</div>
Example 2:
<span class="gl-price__value">€ 139,95</span>
Example 3:
<span id="priceblock_ourprice" class="a-size-medium a-color-price priceBlockBuyingPriceString">79,99 €</span>
I know I can scrape
elements like this:
let html: String = "<a id=1 href='?foo=bar&mid<=true'>One</a> <a id=2 href='?foo=bar<qux&lg=1'>Two</a>";
let els: Elements = try SwiftSoup.parse(html).select("a");
for element: Element in els.array(){
print(try element.attr("href"))
}
But what is the best way to scrape
dynamically? Couldn't find anything on this so I am happy for every help :)
Update
I managed to get the right 'price' if I know the exact 'class-name' :
let url = "https://www.adidas.de/adistar-trikot/CV7089.html"
let className = "gl-price__value"
do {
let html: String = getHTMLfromURL(url: url)
let doc: Document = try SwiftSoup.parse(html)
let price: Element = try doc.getElementsByClass(className).first()!
let priceText : String = try price.text()
result.text = priceText
} catch Exception.Error(let type, let message) {
print(message)
} catch {
print("error")
}
However, I would like to make this work so all 3 examples above work. Right now I am struggling to get the right 'regex' that includes all three examples... Anyone an idea?