英文:
How to get backgroundImage url in cheerio
问题
我试图获取我的Anime-Planet帐户的横幅,用于我的爬虫系统。
我尝试了我所知道的一切,使用cheerio,但我无法获取profileBackground
的background-image
URL。
属性
我尝试了
async function Al() {
const cheerio = require("cheerio");
const url = "https://www.anime-planet.com/users/kyoyacchi";
const {data} = await client.axios.get(url, {
headers: {
"User-Agent":
"Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Mobile Safari/537.36",
},
});
const $ = cheerio.load(data);
return $(".pull-beta.pull-alpha.pullup.editableBanner")
.find(".wrapper")
.find("profileBackground");
}
Al();
这个只返回了头像路径。
英文:
I was trying to get banner of my Anime-Planet account for my scraper system.
I tried everything i have know with cheerio but i couldn't get the profileBackground
s background-image
url.
Properties
I tried
async function Al() {
const cheerio = require("cheerio");
const url = "https://www.anime-planet.com/users/kyoyacchi";
const {data} = await client.axios.get(url, {
headers: {
"User-Agent":
"Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Mobile Safari/537.36",
},
});
const $ = cheerio.load(data);
return $(".pull-beta.pull-alpha.pullup.editableBanner")
.find(".wrapper")
.find("profileBackground");
}
Al();
This one is only returns avatar path.
答案1
得分: 0
我了解到这是$('div[id=profileBackground]')
。
英文:
I learned that it's $('div[id=profileBackground]')
答案2
得分: 0
你可以使用以下代码来获取图像路径:
axios.get(url)
.then(({data: html}) => {
const $ = cheerio.load(html);
const path = $("#profileBackground")
.attr("style")
.match(/background-image: *url *\((.+?)\)/)[1];
console.log(path); // => /images/users/backgrounds/3966485.jpg?t=1660418964
});
使用#some-id
来选择具有id属性的元素。在CSS中,裸单词是指标签名称,所以p
是<p></p>
的选择器。在文档中,id应该是唯一的,所以通常不需要指定父选择器。
上面的正则表达式从background-image: url
后面的括号中提取内容。
英文:
You can retrieve the image path with:
axios.get(url)
.then(({data: html}) => {
const $ = cheerio.load(html);
const path = $("#profileBackground")
.attr("style")
.match(/background-image: *url *\((.+?)\)/)[1];
console.log(path); // => /images/users/backgrounds/3966485.jpg?t=1660418964
});
Use #some-id
to select by the id attribute. In CSS, a bare word refers to a tag name, so p
is the selector for <p></p>
. Ids are supposed to be unique in the document, so you don't usually need to specify parent selectors.
The regex above extracts the contents from the parentheses after background-image: url
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论