python - 使用 selenium 解码 base64 编码的 url
问题描述
我想从网站上抓取一些图像。我检查了网站,一切似乎都很简单,所以我从简单的美汤开始。然后我注意到,图像的格式很奇怪,可能与 base64 相关,所以我尝试对其进行解码,但没有任何结果。我做了一些研究,发现了使用 selenium 的建议,因为图像 url 可以通过 javascript 呈现。所以我用硒尝试了它,但没有成功。
我正在尝试以这种方式获取图像网址:
img = self.browser.execute_script(f"return document.querySelectorAll('picture > img')[{num}]").get_attribute('src')
页面上有 24 张图片,所以我会遍历它们(通过num
)。如果我逐行调试,几个 url 会正确呈现,但是,如果我只是让代码没有断点,我会得到这样的所有 url:
data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7
.
我试图对它进行base64解码,但这对我来说毫无意义。而且它也太短而不能成为实际图像。正确呈现的 url 显示图像实际上不是 gif 文件而是 jpgs。
我还尝试通过 css 选择器(使用纯 beautifulsoup 和 selenium)找到元素,但结果是一样的。
我发现了这个讨论:How to extract img src from web page via lxml in beautifulsoup using python? 但它也没有帮助我。我还没有找到任何动态键(尽管有相似之处——图片有多种尺寸),而且 base64 代码太短而不能成为上面提到的实际图像预览。
如果我在浏览器中检查元素,我会看到正确的 url。有没有办法使用一些漂亮的汤或硒(或其他用于抓取的python框架)来做同样的事情?用 base64 编码的实际数据是什么?
解决方案
如果您查看网站的源代码,您尝试抓取的图像链接存在于另一个标签noscript
中。
您可以使用它们requests
,Beautifulsoup
如下所示:
import requests
from bs4 import BeautifulSoup as bs
url = 'https://eshop.nobilis.cz/aromaterapie/'
res = requests.get(url,headers={'User-Agent': 'Mozilla/5.0'})
soup = bs(res.content, 'html.parser')
images = soup.select('noscript img')
for img in images:
img_link = img.get('src')
img_alt = img.get('alt')
print(img_alt , '==>' , img_link)
输出:
Obrázek kategorie Aromaterapie ==> https://cdn.nobilis.cz/image/custom-w1920-h480-crop/content/aromaterapie_3840x960-bb98d24ff24a2c55.jpg
Keramický difuzér ==> https://cdn.nobilis.cz/image/custom-w225-h250/data/persistent/products/4/6/33/keramicky-difuzer__S8Ru.jpg
Keramická destička ==> https://cdn.nobilis.cz/image/custom-w225-h250/data/persistent/products/4/5/31/n1700-kopie__nQwF.jpg
Aroma difuzér ==> https://cdn.nobilis.cz/image/custom-w225-h250/data/persistent/products/4/6/57/t0328-aroma-difuzer__JYKy.jpg
MINI difuzér ==> https://cdn.nobilis.cz/image/custom-w225-h250/data/persistent/products/5/6/86/01-t0330-mini-difuzer__9RjF.jpg
Zen difuzér ==> https://cdn.nobilis.cz/image/custom-w225-h250/data/persistent/products/5/3/20/t0329-zen-difuzer__IBcR.jpg
Náplně do MINI difuzéru 10 ks ==> https://cdn.nobilis.cz/image/custom-w225-h250/data/persistent/products/4/5/7/t0331s__IqbM.jpg
Aromaterapie na cesty ==> https://cdn.nobilis.cz/image/custom-w225-h250/data/persistent/products/4/5/55/s0103-aromaterapie-na-cesty__0hat.jpg
Keramická amforka ==> https://cdn.nobilis.cz/image/custom-w225-h250/data/persistent/products/5/7/13/keramicka-amforka-kopie__bpFN.jpg
Prostorový difuzér éterických olejů ==> https://cdn.nobilis.cz/image/custom-w225-h250/data/persistent/products/4/7/59/t0320__egh5.jpg
Směs éterických olejů Inspirace ==> https://cdn.nobilis.cz/image/custom-w225-h250/data/persistent/products/7/0/57/e1081b-smes-eterickych-oleju-inspirace__YAb1.jpg
Směs éterických olejů Tantra ==> https://cdn.nobilis.cz/image/custom-w225-h250/data/persistent/products/6/4/63/e2006b-smes-eterickych-oleju-tantra__KeIG.jpg
Éterický olej bio Citron ==> https://cdn.nobilis.cz/image/custom-w225-h250/data/persistent/products/7/4/59/b0015b-bio-citron__KvPJ.jpg
Éterický olej Meduňka ==> https://cdn.nobilis.cz/image/custom-w225-h250/data/persistent/products/6/6/94/e1027-medunka-1-ml__svsg.jpg
Éterický olej Bergamot ==> https://cdn.nobilis.cz/image/custom-w225-h250/data/persistent/products/7/0/27/e0008b-etericky-olej-bergamot__gab2.jpg
Éterický olej Grapefruit ==> https://cdn.nobilis.cz/image/custom-w225-h250/data/persistent/products/6/8/71/e0024b-etericky-olej-grapefruit__J85r.jpg
Éterický olej bio Rozmarýn ==> https://cdn.nobilis.cz/image/custom-w225-h250/data/persistent/products/5/8/12/b0016b-bio-rozmaryn__POvK.jpg
Směs éterických olejů Druhý dech ==> https://cdn.nobilis.cz/image/custom-w225-h250/data/persistent/products/7/3/27/e2002b-smes-eterickych-oleju-druhy-dech__dPzL.jpg
Éterický olej Šalvěj muškátová ==> https://cdn.nobilis.cz/image/custom-w225-h250/data/persistent/products/5/8/93/e0045b-etericky-olej-salvej-muskatova__wAFx.jpg
Éterický olej Cypřiš ==> https://cdn.nobilis.cz/image/custom-w225-h250/data/persistent/products/6/3/55/e0017b-etericky-olej-cypris__RxDS.jpg
Éterický olej Skořice, kůra ==> https://cdn.nobilis.cz/image/custom-w225-h250/data/persistent/products/7/0/60/e0074b-etericky-olej-skorice-kura__tK0h.jpg
Éterický olej Geranium ==> https://cdn.nobilis.cz/image/custom-w225-h250/data/persistent/products/6/2/71/e1057b-etericky-olej-geranium__dCRQ.jpg
Éterický olej Konopí ==> https://cdn.nobilis.cz/image/custom-w225-h250/data/persistent/products/5/7/67/e0154h-konopi-1-ml__b2oW.jpg
Růže v jojobovém oleji ==> https://cdn.nobilis.cz/image/custom-w225-h250/data/persistent/products/6/7/24/n1010c-ruze-v-jojobe-20-ml__jzLM.jpg
Éterický olej bio Tymián linalol ==> https://cdn.nobilis.cz/image/custom-w225-h250/data/persistent/products/6/3/82/b0005a-bio-tymian-linalol__8IFa.jpg
推荐阅读
- mongo-cxx-driver - 从字符串创建 bsoncxx::builder::basic::document?
- sbt - 在多项目 SBT 构建中,如何运行 1 个主程序?
- python - 使用预定义的顺序按索引列重新排序 csv 文件
- .net - DbSet 的 Add 方法是否会导致立即查询 EF 中的数据库?
- python - 机器学习模型可以输出多个决策吗?
- javascript - Input type range with IE11 showing max/min integer
- kotlin - 如何将自定义属性双向绑定到文本字段?
- python - 如何使用 Numpy 比较数组的多维元素是否存在于另一个数组中?
- python - 如何拆分由“;”分隔的文件行中的单词
- listview - Flutter ListTile 前导图和标题对齐