【问题标题】:Trying to scrape data using selenium>试图使用selenium>刮掉数据>
【发布时间】:2019-12-15 08:34:19
【问题描述】:

我正在尝试使用 selenium 从此代码中获取 jpg。我已经成功了 找到链接以单击以获取我的 jpg 所在的位置。 (运气不好! 我是硒的新手)。当它点击时,所有的窗口都会打开。它是 与 Scrapy 相比,真的很慢,所以如果有人能告诉我更快的方法 那太好了。

我要抓取的网站是 www.rosegal.com。类别 我刮的是加大码的背心。这第一页有 60 个产品 在上面。如果您单击这些产品,它会将您带到产品页面,其中 您可以选择所需的尺寸、颜色和数量。

每种颜色都有自己的图像,所以我要做的是点击第一个 颜色,抓取与该颜色相关的第一个大图像,然后 第二种颜色刮掉该图像,依此类推。

我已经点击了我想要抓取的数据。我已经进口了 硒。我打电话给chromedriver。命名驱动程序,我已经加载了网址 我想开始并点击我要抓取的产品,然后我 点击我的 jpg 所在的颜色,现在我准备好获取我的 jpg。

    from selenium import webdriver

    chrome_path = r"C:/Users/daver/Downloads/chromedriver_win32 
    (4)/chromedriver.exe"

    driver = webdriver.Chrome(chrome_path)

   driver.get("https://www.rosegal.com/plus-size-tank-tops-482/")

   driver.find_element_by_xpath("""//* 
   [@id="js_proList"]/ul[1]/li[1]/div/div/p""").click()

   #This is what I tried this to get my jpg:   

  image1_element = driver.find_element_by_xpath("""//* 
  [@id="goods_thumb_content"]/ul/li[1]/img""")

  print("image1_element")
  print(image1_element)

  image1_element = driver.find_element_by_xpath("""//* 
  [@id="goods_thumb_content"]/ul/li[2]/img""")

  print("image2_element")
  print(image2_element)

  image1_element = driver.find_element_by_xpath("""//* 
  [@id="goods_thumb_content"]/ul/li[3]/img""")

  print("image3_element")
  print(image3_element)





   **This is the code the jpg is embedded:**

<div id="js_zoom_img" style="position: relative; overflow: hidden;">  
    <img data-zoomimg="https://gloimg.rglcdn.com/rosegal/pdm-product- 
    pic/Clothing/2019/06/18/source-img/20190618173639_71567.jpg" 

尝试获取此 jpg:

https://gloimg.rglcdn.com/rosegal/pdm-product- 图片/服装/2019/06/18/source-img/20190618173639_71567.jpg

还尝试返回包含 60 种产品的页面,以便我可以 刮掉其他产品。

【问题讨论】:

  • 很抱歉重复了一些问题。 :)

标签: selenium web-scraping


【解决方案1】:

以下代码将帮助您单击每种颜色并打印与其关联的所有图像。

#To click on each color
Colors = driver.find_elements_by_xpath("//p[@class='attr-choose clearfix goods_property_color']//a")

for color in Colors:
    print("Clicking on color: ",color.get_attribute('data-value'))
    color.click()
    time.sleep(2)
    #now collect all the image urls for the color
    images = driver.find_elements_by_xpath("//div[@id='goods_thumb_content']//li")
    print("Total images",len(images))
    for image in images:
        print(image.get_attribute('data-bigimg'))

【讨论】:

  • 我需要导入时间吗?以及如何返回列出 60 种产品的页面?
  • 是的。您将需要导入时间。您可以使用 driver.back() 导航回来。但是,您必须维护一个包含 60 种产品的运行列表,并在每次返回时找到要单击的元素。这是为了避免过期元素异常。
  • 不要使用固定的 sleep()
猜你喜欢
  • 1970-01-01
  • 2018-12-01
  • 1970-01-01
  • 2015-04-02
  • 1970-01-01
  • 1970-01-01
  • 2020-10-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多