如何打开div类中的所有href?

鸡屁股

我是python和所有事物的新手,并且我希望解析div类中的所有href。我的目标是创建一个程序来打开div类中的所有链接,以便能够保存与href相关联的照片。

链接:https//www.opi.com/shop-products/nail-polish-powders/nail-lacquer

我要解析的部分是“ div-id:all_nail_lacquer”

到目前为止,我已经能够获取所有href,而这是到目前为止的结果:

import urllib
import urllib.request
from bs4 import BeautifulSoup

theurl = "https://www.opi.com/shop-products/nail-polish-powders/nail-lacquer"
thepage = urllib.request.urlopen(theurl)
soup = BeautifulSoup(thepage, "html.parser")

print(soup.title.text)

nail_lacquer = (soup.find('div', {"id":"all_nail_lacquer"}))

"""
for nail_lacquer in soup.find_all('div'):
    print(nail_lacquer.findAll('a')
"""

for a in soup.findAll('div', {"id":"all_nail_lacquer"}):
    for b in a.findAll('a'):
        print(b.get('href'))
安德烈·凯斯利(Andrej Kesely)

要打印图像链接(甚至是高分辨率图像)和标题,可以使用以下脚本:

import urllib
import urllib.request
from bs4 import BeautifulSoup

theurl = "https://www.opi.com/shop-products/nail-polish-powders/nail-lacquer"
thepage = urllib.request.urlopen(theurl)
soup = BeautifulSoup(thepage, "html.parser")

for img in soup.select('#all_nail_lacquer [typeof="foaf:Image"][data-src]'):
    print(img['data-src'])
    print(img['data-src'].replace('shelf_image', 'photos')) # <-- this is URL to hi-res image
    print(img['title'])
    print('-' * 80)

印刷品:

https://www.opi.com/sites/default/files/styles/product_shelf_image/public/baby-take-a-vow-nlsh1-nail-lacquer-22850011001_0_0.jpg?itok=3b2ftHzc
https://www.opi.com/sites/default/files/styles/product_photos/public/baby-take-a-vow-nlsh1-nail-lacquer-22850011001_0_0.jpg?itok=3b2ftHzc
Baby, Take a Vow
--------------------------------------------------------------------------------
https://www.opi.com/sites/default/files/styles/product_shelf_image/public/suzi-without-a-paddle-nlf88-nail-lacquer-22006698188_21_0.jpg?itok=mgi1-rz3
https://www.opi.com/sites/default/files/styles/product_photos/public/suzi-without-a-paddle-nlf88-nail-lacquer-22006698188_21_0.jpg?itok=mgi1-rz3
Suzi Without a Paddle
--------------------------------------------------------------------------------
https://www.opi.com/sites/default/files/styles/product_shelf_image/public/coconuts-over-opi-nlf89-nail-lacquer-22006698189_24_1_0.jpg?itok=yasOZA4l
https://www.opi.com/sites/default/files/styles/product_photos/public/coconuts-over-opi-nlf89-nail-lacquer-22006698189_24_1_0.jpg?itok=yasOZA4l
Coconuts Over OPI
--------------------------------------------------------------------------------
https://www.opi.com/sites/default/files/styles/product_shelf_image/public/no-tan-lines-nlf90-nail-lacquer-22006698190_20_1_0.jpg?itok=ot_cu8c5
https://www.opi.com/sites/default/files/styles/product_photos/public/no-tan-lines-nlf90-nail-lacquer-22006698190_20_1_0.jpg?itok=ot_cu8c5
No Tan Lines
--------------------------------------------------------------------------------


...and so on.

编辑:要将图像保存到磁盘,您可以使用此脚本:

import requests
from bs4 import BeautifulSoup

theurl = "https://www.opi.com/shop-products/nail-polish-powders/nail-lacquer"
thepage = requests.get(theurl)
soup = BeautifulSoup(thepage.content, "html.parser")

i = 1
for img in soup.select('#all_nail_lacquer [typeof="foaf:Image"][data-src]'):
    u = img['data-src'].replace('shelf_image', 'photos')
    with open('img_{:04d}.jpg'.format(i), 'wb') as f_out:
        print('Saving {}'.format(u))
        f_out.write(requests.get(u).content)
    i += 1

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何从Python Selenium中的类提取所有href?

如何选择 div 和 href 中的所有文本

检查并清除容器中具有特定类的所有打开的div

如何选择一个类中的所有 <div>

jQuery:如何选择所有特定于div的类并将div中的所有内容附加到新的div

我如何獲得此 div 中的所有 href 鏈接

访问深度类层次结构中的所有href链接

如何获取html中属于某个类的所有div,并且除非它们是特定的div,否则如何更改其类?

BeautifulSoup 找到 div > span > a 中的所有 title 和 href

python BeautifulSoup在div的子代中获取所有href

如何检查所有其他div是否在输入事件中包含特定类?

如何提取HTML文件中的所有链接(href)?

如何在python中打开目录中的所有文件?

如何在jQuery中隐藏除第一类以外的每个类的所有div

如何从所有div中(不仅仅是同级兄弟)中删除带有jQuery的类?

如何使用类更改div中所有按钮的tabindex

如何增加某个类的所有div的字体大小

向div中的所有范围添加活动类

如何打开所有标志?

div:href和所有链接

如何重新打开Chrome中的所有窗口?

如何找到在给定目录中打开的所有文件?

如何在IntelliJ中合并所有打开的窗口?

如何打开Java中以特定前缀开头的所有文件?

如何在PhpStorm中打开所有修改的文件

如何在Arduino中打开所有ADC

如何终止在子流程中打开的所有子流程

如何正确地将CSS样式应用于具有特定类的div中的所有图像?

如何为 2 个子 div 添加包装 div(所有子 div 具有相同的类)