简体   繁体   English

如何使用 Python 请求模块检索变量的内容?

[英]How do I retrieve a variable's content using Python Requests module?

I started a little personal project just for fun.我开始了一个小小的个人项目只是为了好玩。 I hope posting what I'm doing here doesn't break any of the local rules.我希望在这里发布我正在做的事情不会违反任何当地规则。 If so, let me know and I'll take the question down.如果是这样,请告诉我,我会记下这个问题。 No need to flag me for anything无需为任何事情标记我

I'm trying to pull the background image URL of my chromium homepage.我正在尝试提取我的铬主页的背景图像 URL。 Just for reference, the URL is https://www.mystart.com/new-tab/newtab/ When going to this page, nice background images are loaded.仅供参考,URL 是https://www.mystart.com/new-tab/newtab/当转到此页面时,会加载漂亮的背景图像。 I'm trying to grab those images for personal, not commercial, use.我试图获取这些图像用于个人而非商业用途。

What I've traced down is that the page listed above calls out to another similar page: https://www.mystart.com/new-tab/newtab/newtab/ Currently, on line #1622 through #1636, two significant lines read:我追查到的是,上面列出的页面调用了另一个类似的页面: https://www.mystart.com/new-tab/newtab/newtab/目前,在 #1622 到 #1636 行,两个重要的行读:

var fastload    = JSON.parse(localStorage.getItem('FASTLOAD_WALLPAPER_557b2c52a6fde1413ac3a48a'))
...
var url = fastload.info.cache_url || fastload.info.data_uri || fastload.info.image;

The value returned in the url is the URL to the background image. url中返回的值是 URL 到背景图像。 If I drop into the Chromium console and use: console.log(url) , I see the exact data I'm trying to scrape.如果我进入 Chromium 控制台并使用: console.log(url) ,我会看到我试图抓取的确切数据。 I'm wondering how I do that through python, since the actual textValue of url is not seen.我想知道如何通过 python 做到这一点,因为没有看到url的实际 textValue。

I have looked all over to try to find the localStorage object definition with no luck.我已经四处寻找,试图找到localStorage object 定义,但没有运气。 I'm pulling the page with result = requests.get("https://www.mystart.com/new-tab/newtab/newtab/");我正在使用result = requests.get("https://www.mystart.com/new-tab/newtab/newtab/"); and then looking through result.text .然后查看result.text I've also tried using BeautifulSoup to parse through things, not that this is really any different, but still not getting the results I'm looking for.我也尝试过使用 BeautifulSoup 来解析事物,并不是说这真的有什么不同,但仍然没有得到我想要的结果。

Being that I'm a hobbyist coder, I feel like I'm missing something simple.由于我是一个业余爱好者,我觉得我错过了一些简单的东西。 I've searched for answers, but I must be using the wrong keywords.我已经搜索了答案,但我必须使用错误的关键字。 I'm finding a lot of answers for parsing the urls that can be read, but not from the contents of a variable.我找到了很多解析可以读取的 url 的答案,但不是从变量的内容中读取的。

if you look at the requests being made, there is JSON response with info for 350 images.如果您查看正在发出的请求, 则会出现 JSON 响应,其中包含 350 张图像的信息。 image_id is used in the url, eg https://gallery.mystartcdn.com/mystart/images/<image_id>.jpeg so for id=154_david-wilson-moab : image_id用于 url,例如https://gallery.mystartcdn.com/mystart/images/<image_id>.jpeg所以对于id=154_david-wilson-moab

https://gallery.mystartcdn.com/mystart/images/154_david-wilson-moab.jpeg https://gallery.mystartcdn.com/mystart/images/154_david-wilson-moab.jpeg

Parse the JSON and get url for all images.解析 JSON 并获取所有图像的 url。

Note: this is not an answer of your question, but it looks like XY problem - this solves the underlying problem of retrieving image urls.注意:这不是您问题的答案,但它看起来像 XY 问题 - 这解决了检索图像 url 的潜在问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM