json - 使用 serde crate 在 rust 中使用选择和排序数据进行 Web 抓取
问题描述
我正在尝试使用rust 中的 crate来抓取网站。select
这是网站结构:
example.com/category-list/
example.com/cat/programming-questions/
qtitle = "How to become a programmer"
qid = 2
example.com/cat/networking-questions/
qtitle = "New question"
qid = 3
qtitle = "Other question"
qid = 4
刮板代码:
#[derive(Serialize, Deserialize, Debug)]
pub struct Question {
q_title: Vec<String>,
q_id: Vec<String>,
q_link: Vec<String>,
}
let mut questions_vector= Vec::new();
for response in reponse_list.iter() {
let mut q_title: Vec<String> = Vec::new();
Document::from(resp.as_str())
.select(Class("qTitle"))
.for_each(|f| q_title.push(f.to_string()));
let mut q_id: Vec<String> = Vec::new();
Document::from(resp.as_str())
.select(Class("qid"))
.for_each(|f| q_id.push(f.to_string()));
let mut q_link: Vec<String> = Vec::new();
Document::from(resp.as_str())
.select(Name("a"))
.filter_map(|f| f.attr("href"))
.for_each(|f| q_link.push(f.to_string()));
let question = Question {
q_title: q_title,
q_id: q_id,
q_link: q_link,
};
questions_vector.push(question);
}
println!("{}", serde_json::to_string(&questions_vector).unwrap());
我的代码的输出:
{
"q_title": [
"How to become a programmer",
],
"q_id": [
"2",
],
"q_link": [
"https://example.com/q/How-to-become-a-programmer"
]
},
{
"q_title": [
"New question",
"Other question"
],
"q_id": [
"3","4"
],
"q_link": [
"https://example.com/q/New-question",
"https://example.com/q/Other-question"
]
}
有目的的输出:
{
"q_title": [
"How to become a programmer",
],
"q_id": [
"2",
],
"q_link": [
"https://example.com/q/How-to-become-a-programmer"
]
},
{
"q_title": [
"New question",
],
"q_id": [
"3",
],
"q_link": [
"https://example.com/q/New-question",
]
},
{
"q_title": [
"Other question"
],
"q_id": [
"4",
],
"q_link": [
"https://example.com/q/Other-question"
]
},
我需要重构我的代码以实现有目的的输出
解决方案
推荐阅读
- shell - 为什么在 shell 脚本中运行时游标语句在 isql(sybase) 中不起作用
- windows - 如何使用 Go 在 Windows 上获取文件所有者?
- javascript - 为什么我在 vuex 商店中的初始状态未定义?
- mysql - 拆分字符串字段后添加行号
- python - 在python中导入图像
- python - 在 Python 中连接 C 结构
- matlab - 如何分别计算每个尺度级别的二维逆平稳小波变换?
- firebase - Firebase 功能的源图支持?
- javascript - SVG 和文本在 Safari 上不起作用
- django - 在博客文章中添加下一个/上一个按钮