r - 提取 div#data-old-hires 中后续图像的链接
问题描述
在一些帮助下,我能够提取 url 的登陆图像/主图像。但是,我也希望能够提取后续图像
require(rvest)
url <-"https://www.amazon.in/Livwell-Multipurpose-MultiColor-Polka-
Lunch/dp/B07LGTPM3D/ref=sr_1_1_sspa?ie=UTF8&qid=1548701326&sr=8-1-
spons&keywords=lunch+bag&psc=1"
webpage <- read_html(url)
r <- webpage %>%
html_nodes("#landingImage") %>%
html_attr("data-a-dynamic-image")
imglink <- strsplit(r, '"')[[1]][2]
print(imglink)
这为主图像提供了正确的输出。但是,当我翻转到同一产品的其他图像时,我想提取链接。本质上,我希望输出具有以下链接:
1. https://images-na.ssl-images-amazon.com/images/I/81bF%2Ba21WLL。UY500.jpg _
https://images-na.ssl-images-amazon.com/images/I/81HVwttGJAL。UY500.jpg _
https://images-na.ssl-images-amazon.com/images/I/81Z1wxLn-uL。UY500.jpg _
https://images-na.ssl-images-amazon.com/images/I/91iKg%2BKqKML。UY500.jpg _
https://images-na.ssl-images-amazon.com/images/I/91zhpH7%2B8gL。UY500.jpg _
非常感谢
解决方案
根据底部要求的 Python 脚本。为了使其适用于各种语言,答案分为两部分。1) 可以使用 R/Python/许多其他语言执行的步骤的高级伪代码描述 2) python 示例。
R 脚本以获取末尾显示的字符串(过程的步骤 1-3 )。
1)过程:
- 通过 GET 请求获取 html
- 正则表达式从脚本标签之一中取出一个子字符串,这实际上是页面上的 jquery 用来提供来自 json 的图像链接的内容
正则表达式模式是
jQuery\.parseJSON\(\'(.*)\'\);
解释是:
基本上,包含的 json 对象是在 characters{
之前和之前收集的。这会将这个json 对象提取为字符串。第一个捕获组 (.*) 的使用从开始字符串和结束字符串(不包括任何一侧)之间收集。"dataInJson"
')
- 第一个匹配是唯一需要的,因此必须从返回的匹配中提取第一个。然后使用一个 json 解析库来处理这个问题,该库可以接受一个字符串并返回一个 json 对象
- 该 json 对象通过键循环访问(在 Python 的情况下,结构是字典 - R 会略有不同)
colorImages
,以生成(产品的)颜色,这些颜色又用于访问实际的 url 本身。
颜色:
图像的嵌套级别:
2) Python 中显示的那些步骤
import requests #library to handle xhr GET
import re #library to handle regex
import json
headers = {'User-Agent' : 'Mozilla/5.0', 'Referer':'https://www.amazon.in/Livwell-Multipurpose-MultiColor-Polka-%20Lunch/dp/B07LGTPM3D/ref=sr_1_1_sspa?ie=UTF8&qid=1548701326&sr=8-1-%20spons&keywords=lunch+bag&psc='}
r = requests.get('https://www.amazon.in/Livwell-Multipurpose-MultiColor-Polka-%20Lunch/dp/B07LGTPM3D/ref=sr_1_1_sspa?ie=UTF8&qid=1548701326&sr=8-1-%20spons&keywords=lunch+bag&psc=1', headers = headers)
p1 = re.compile(r'jQuery\.parseJSON\(\'(.*)\'\);')
data = p1.findall(r.text)[0]
json_source = json.loads(data)
for colour in json_source['colorImages']:
for image in json_source['colorImages'][colour]:
print(image['large'])
输出:
所有颜色的产品链接 - 仅限大图链接(因此 url 看起来略有不同,数量更多,但图片相同)
用于正则表达式输出所需字符串并生成 JSON 的 R 脚本:
library(rvest)
library( jsonlite)
library(stringr)
con <- url('https://www.amazon.in/Livwell-Multipurpose-MultiColor-Polka-%20Lunch/dp/B07LGTPM3D/ref=sr_1_1_sspa?ie=UTF8&qid=1548701326&sr=8-1-%20spons&keywords=lunch+bag&psc=1', "rb")
page = read_html(con)
page %>%
html_nodes(xpath=".//script[contains(., 'colorImages')]")%>%
html_text() %>% as.character %>% str_match(.,"jQuery\\.parseJSON\\(\\'(.*)\\'\\);") -> res
json = fromJSON(res[,2][2])
他们已经更新了页面,所以现在只需使用:
Python:
import requests #library to handle xhr GET
import re #library to handle regex
headers = {'User-Agent' : 'Mozilla/5.0', 'Referer':'https://www.amazon.in/Livwell-Multipurpose-MultiColor-Polka-%20Lunch/dp/B07LGTPM3D/ref=sr_1_1_sspa?ie=UTF8&qid=1548701326&sr=8-1-%20spons&keywords=lunch+bag&psc='}
r = requests.get('https://www.amazon.in/Livwell-Multipurpose-MultiColor-Polka-%20Lunch/dp/B07LGTPM3D/ref=sr_1_1_sspa?ie=UTF8&qid=1548701326&sr=8-1-%20spons&keywords=lunch+bag&psc=1', headers = headers)
p1 = re.compile(r'"large":"(.*?)"')
links = p1.findall(r.text)
print(links)
回复:
library(rvest)
library(stringr)
con <- url('https://www.amazon.in/Livwell-Multipurpose-MultiColor-Polka-%20Lunch/dp/B07LGTPM3D/ref=sr_1_1_sspa?ie=UTF8&qid=1548701326&sr=8-1-%20spons&keywords=lunch+bag&psc=1', "rb")
page = read_html(con)
res <- page %>%
html_nodes(xpath=".//script[contains(., 'var data')]")%>%
html_text() %>% as.character %>%
str_match_all(.,'"large":"(.*?)"')
print(res[[1]][,2])
推荐阅读
- c# - 为什么这段代码会挂在`await TransformBlock.Completion`上
- swift - 如何在附加到 UITextField 的 UIPickerView 上设置 selectRow
- mrtk - 是否可以在构建中强制使用 MRTK 的 InputSimulationService?
- react-select - 移除对MouseOut React-Select 的关注
- memory - FPGA Stratix 3 存储器能否处理大量数据?
- javascript - Javascript函数计算自以来经过的年数
- python - Pandas 中的条件累积和
- c++ - 如何调用存储在数组中的数组的值
- javascript - 赛普拉斯测试:如何比较 AJAX 调用前后的元素数量
- c++ - 如何使用 DirectX 11 渲染多个网格