【问题标题】:Python Selenium Get multiple images with the same class and save itPython Selenium 获取同一个类的多张图片并保存
【发布时间】:2021-04-27 11:08:10
【问题描述】:

所以,我想创建一个机器人,在 Reddit 上下载多个(全部)模因(图像的类是 _2_tDEnGMLxpM6uOa2kaDB3 ImageBox-image media-element _1XWObl-3b9tPy64oaG6fax)

好的,这是我的代码:

from selenium import webdriver
import time
import pyautogui
import urllib
browser = webdriver.Chrome(executable_path=r"chromedriver_win32/chromedriver.exe")
browser.get('https://www.reddit.com/r/memes/new/')
time.sleep(3)
pyautogui.click(375,187)
findclass = browser.find_elements_by_class_name("_2_tDEnGMLxpM6uOa2kaDB3 ImageBox-image media-element _1XWObl-3b9tPy64oaG6fax")
for i in findclass:
    try:
        src = findclass.get_attribute('src')
        urllib.urlretrieve(src, "meme.png")
        print("found it!")
    except Exception as e:
        print(e)

在我的输出中什么都没有,没有错误什么都没有 idk 出了什么问题 xd ???

【问题讨论】:

  • 你试过只安装一个吗?从小处着手,它可能看不到图像。

标签: python python-3.x selenium bots


【解决方案1】:

你的意思是获取所有的 src 标签吗?还添加了如何使用允许/阻止部分的通知。

options = Options()
options.add_argument("--disable-infobars")
options.add_argument("start-maximized")
options.add_argument("--disable-extensions")

# Pass the argument 1 to allow and 2 to block
options.add_experimental_option("prefs", { 
    "profile.default_content_setting_values.notifications": 1 
})
browser = webdriver.Chrome(executable_path=r"chromedriver_win32/chromedriver.exe",options=options)
browser.get('https://www.reddit.com/r/memes/new/')
elems=[link.get_attribute("src")for link in browser.find_elements_by_xpath("//img[contains(@class,'ImageBox-image')]")]
print(elems)

导入

from selenium.webdriver.chrome.options import Options

输出:

['https://preview.redd.it/4g91j26pdzc61.jpg?width=640&crop=smart&auto=webp&s=df817665333696fa9d3bc61c648f436c7d4866cc', 'https://external-preview.redd.it/tTv773TGtaaR1SZje-nRpxTMJqU4etx426sLNbbnPHI.jpg?width=640&crop=smart&auto=webp&s=60be46511f8dc95ba81cc196ef8a4127eed8cf2e', 'https://preview.redd.it/q6fpcqgldzc61.jpg?width=640&crop=smart&auto=webp&s=c1376540801c30af074f08df96d2c30b3f0f0b53', 'https://preview.redd.it/risq0ndndzc61.jpg?width=640&crop=smart&auto=webp&s=14297374d2ce1a5cdd757e7003ecad159abca345', 'https://preview.redd.it/e3ak8tzldzc61.jpg?width=640&crop=smart&auto=webp&s=155d24158cf009988411013d0759f48da1034a53', 'https://preview.redd.it/vpkai4wkdzc61.jpg?width=640&crop=smart&auto=webp&s=c01eb9e610fb228b5b3a72bdb9b4e2b61b2e2835', 'https://i.redd.it/ymtqptejdzc61.jpg', 'https://preview.redd.it/0hx2vjmgdzc61.jpg?width=640&crop=smart&auto=webp&s=ca24c436b8c9d9b1fba04c3dc5749b9a34e319fb', 'https://preview.redd.it/untwq4iddzc61.jpg?width=640&crop=smart&auto=webp&s=b2de8dafda5359946ce35d7acc35c0ba50af0b46']

【讨论】:

  • 嘿!感谢 Anwser 这就是我需要的感谢您添加通知部分 xd!但是有没有办法过滤掉广告?广告中有一个徽章,其 class_2oEYZXchPfHwcf9mTMGMg8 是一个跨度。那么有没有办法制作一个 if 并说出类似 if 类是否带有图像的内容:dont get src.
  • If link.get_attribute("class") != "whatever" : 在链接前面。您可能可以使 xpath 更加集中
  • 如果链接只在 div 中,只需添加 //div/img,span/img 就会消失。
猜你喜欢
  • 1970-01-01
  • 2019-04-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-08
  • 1970-01-01
  • 2014-04-02
  • 2015-04-14
  • 1970-01-01
相关资源
最近更新 更多