web-scraping - Using Google Sheets as a Web Scraper for a Site with Java
问题描述
I have been trying to use IMPORTXML()
to scrape information from the following website.
https://libbyapp.com/library/lapl/search/audiobooks/query-john%20scalzi/language-en/page-1
After no success with the command and some digging online, it seems that this command will not work because the website uses java to render the information.
Is there any other command or method within google sheets to scrape some information from this web page? Specifically I'm trying to get the names and authors of the first 3 books listed.
解决方案
不,不是Java。JavaScript。非常不同的东西。
幸运的是,该站点有一个 JSON API!您可以在浏览器的网络监视器中亲自查看。这意味着没有纯公式,但我们可以编写一个简单的用户脚本(工具 > 脚本编辑器)。
我们制作这个用户定义的函数(粘贴到编辑器中):
function getAuthors(url, showHeaders)
{
let jsondata = UrlFetchApp.fetch(url);
let object = JSON.parse(jsondata.getContentText());
let bookData = showHeaders ?
[ ['Title', 'Author(s)'] ] :
[];
bookData = bookData.concat(
object.items.map(item => (
// Title is always first, followed by any authors:
[item.title].concat(
item.creators
// Add more creator roles here:
.filter(creator => creator.role === 'Author')
// Take creator's name
.map(author => author.name)
// Comma-separated
.join(',')
))
)
);
return bookData;
}
使用网络监视器,我确定感兴趣的 URL 是
https://thunder.api.overdrive.com/v2/libraries/lapl/media?overdriveFormats=true&mediaType=audiobook&query=john%20scalzi&language=en&page=1&perPage=24&x-client-id=dewey
然后在您的工作表中,您可以使用以下公式:
=getAuthors(A1)
或者
=getAuthors(A1, 1)
如果你想要标题。
如果你想要前三个,那么只需使用ARRAY_CONSTRAIN
截断表。
推荐阅读
- java - 在来自 OpenJDK 11 的源代码上运行 DocumentationTool
- javascript - 试图在onClick上添加一个列表项删除线,但它破坏了我的提交按钮
- encryption - 使用 openssl C API 加密的文件无法通过命令行 openssl 解密
- javascript - 单击按钮时出现“找不到文件”错误
- excel - 如何修复 700K 行范围的溢出
- r - 如何从两个数据框中查找缺失的元素
- python - 对于 tensorflow.keras.models.Sequential.predict 中的多类多标签问题,是否可能只得到 0 和 1?
- python - 如何修复此错误:AttributeError:'list' object has no attribute 'items'?
- ruby-on-rails - Rails 5 Javascript 如何使用供应商资产?
- c# - 我正在尝试在ideone上编译