2021-05-14 11:09:21 +03:00
|
|
|
import net.http
|
|
|
|
import net.html
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
// http.fetch() sends an HTTP request to the URL with the given method and configurations.
|
|
|
|
config := http.FetchConfig{
|
|
|
|
user_agent: 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:88.0) Gecko/20100101 Firefox/88.0'
|
|
|
|
}
|
2021-08-17 18:36:10 +03:00
|
|
|
resp := http.fetch(http.FetchConfig{ ...config, url: 'https://tuicool.com' }) or {
|
2021-05-14 11:09:21 +03:00
|
|
|
println('failed to fetch data from the server')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// html.parse() parses and returns the DOM from the given text.
|
2022-07-19 12:52:42 +03:00
|
|
|
mut doc := html.parse(resp.body)
|
2023-06-27 20:10:31 +03:00
|
|
|
// html.DocumentObjectModel.get_tags_by_attribute_value() retrieves all tags in the document that have the given attribute name and value.
|
|
|
|
tags := doc.get_tags_by_attribute_value('class', 'list_article_item')
|
2021-05-14 11:09:21 +03:00
|
|
|
for tag in tags {
|
2022-04-07 18:18:10 +03:00
|
|
|
el := tag.children[1].children[0].children[0].children[0]
|
|
|
|
href := el.attributes['href'] or { panic('key not found') }
|
|
|
|
title := el.attributes['title'] or { panic('key not found') }
|
2022-11-15 16:53:13 +03:00
|
|
|
println('href: ${href}')
|
|
|
|
println('title: ${title}')
|
2021-05-14 11:09:21 +03:00
|
|
|
println('')
|
|
|
|
}
|
|
|
|
}
|