【问题标题】:Scraping nested element on e-commerce website在电子商务网站上抓取嵌套元素
【发布时间】:2021-02-10 21:14:54
【问题描述】:

当我访问特定的产品页面时,我试图用 Selenium 从 Target 的网站上抓取产品 img url,但没有返回任何内容。

这是我的那部分代码:

# ADD THE IMAGE URL
    j = 0
    found = False
    while(j < 5 and not found):
        try:
            img_panel = driver.find_element_by_class_name('slideDeckPicture')
            img_panel = img_panel.find_element_by_tag_name('img')
            img_name = img.get_attribute('alt')
            img_url = img_panel.get_attribute('src')

            # img_urls.append(img_url)
            line += ',"' + img_url + '"'
            found = True
            break
        # if it can't find the image, it probably hasn't loaded. wait and try again.
        except:
            j += 1
            time.sleep(4)
            # img_urls.append('NO URL')
            # pass
    # if we've tried 5 times add no url
    if found == False:
        line += ',NO IMG URL'

HTML 截图:

Link to example product

【问题讨论】:

    标签: python web-scraping data-science


    【解决方案1】:

    url 列表包含您要查找的 url:

    url = "https://www.target.com/p/revolution-beauty-conceal-define-concealer-0-11-fl-oz/-/A-82003638?preselect=81551727#lnk=sametab"
    headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:86.0) Gecko/20100101 Firefox/86.0"}
    resp = rq.get(url, headers=headers)
    soup = bs(resp.content)
    
    divs_img = soup.find_all("div", attrs={"data-test": "product-image"})[0]
    urls = [i["src"] for i in divs_img.find_all("img") if i["src"].startswith("https")]
    

    【讨论】:

    • 我使用的是 selenium,所以会不会有所不同?
    • 你可以同时使用 selenium 和 BeautifulSoup。因此,当您获取源页面并将其包含在 bs(....) 中时,以下代码保持不变。
    猜你喜欢
    • 1970-01-01
    • 2019-06-21
    • 1970-01-01
    • 2019-09-11
    • 1970-01-01
    • 1970-01-01
    • 2011-06-11
    • 2020-12-23
    • 1970-01-01
    相关资源
    最近更新 更多