我们目前正在尝试将代码从Python网络抓取器转移到Node.js网络抓取器。来源为Pastebin API。抓取时,响应是一个javascript对象,如下所示:
[
{
scrape_url: 'https://scrape.pastebin.com/api_scrape_item.php?i=FD1BhNuR',
full_url: 'https://pastebin.com/FD1BhNuR',
date: '1580299104',
key: 'FD1BhNuR',
size: '19363',
expire: '0',
title: 'Weight Loss',
syntax: 'text',
user: 'loscanary'
}
]我们的Python脚本使用requests库从Pastebin的API请求数据并访问粘贴的实际主体,除了上面的参数之外,我们还遍历第一个条目并检索其文本值。以下是摘录:
response = requests.get("https://scrape.pastebin.com/api_scraping.php?limit=1")
parsed_json = response.json()
print(parsed_json)
for individual in parsed_json:
p = requests.get(individual['scrape_url'])
text = p.text
print(text)这将返回粘贴的实际正文,然后我们可以搜索这些正文以获取更多关键字。
在Node中,我不知道如何像在requests.text中那样检索"scrape_url“参数的相同文本值。我尝试过使用axios和request,但我能得到的最多的是访问"scrape_url“参数,如下所示:
const scrape = async () => {
try {
const result = await axios.get(pbUrl);
console.log(result.data[0].scrape_url);
} catch (err) {
console.error(err);
}
}
scrape();我如何从Python Requests库和循环中获得与.text相同的结果?
发布于 2020-01-31 20:21:38
下面是一个如何做到这一点的示例(正如Olvin Roght所提到的)
const scrape = async () => {
try {
const result = await axios.get(pbUrl);
result.data.forEach(async (individual) => {
const scrapeUrl = individual['scrape_url'];
const response = await axios.get(scrapeUrl);
const text = response.data;
console.log("this is the text value from the url:", text);
});
} catch (err) {
console.error(err);
}
}
scrape();https://stackoverflow.com/questions/59968562
复制相似问题