簡體   English   中英

無法使用Mechanize識別正確的CSS選擇器

[英]Can't Identify Proper CSS Selector to Scrape with Mechanize

我建立了一個網站刮板已成功拉動幾乎所有我需要縮小網頁我看的。 目標是拉出與在特定URL處找到的所有咖啡相關聯的特定圖像的URL。

我為完成抓取而定義的rake任務如下:

mechanize = Mechanize.new
mechanize.get(url) do |page|
    page.links_with(:href => /products/).each do |link|
        coffee_page = link.click

            bean = Bean.new

            bean.acidity = coffee_page.css('[data-id="acidity"]').text.strip.gsub("acidity ","")
            bean.elevation = coffee_page.css('[data-id="elevation"]').text.strip.gsub("elevation ","")
            bean.roaster_id = "2"
            bean.harvest_season = coffee_page.css('[data-id="harvest"]').text.strip.gsub("harvest ","")
            bean.price = coffee_page.css('.price-wrap').text.gsub("$","")
            bean.roast_profile = coffee_page.css('[data-id="roast"]').text.strip.gsub("roast ","")
            bean.processing_type = coffee_page.css('[data-id="process"]').text.strip.gsub("process ","")
            bean.cultivar = coffee_page.css('[data-id="cultivar"]').text.strip.gsub("cultivar ","")
            bean.flavor_profiles = coffee_page.css('.price-wrap+ p').text.strip
            bean.country_of_origin = coffee_page.css('#pdp-order h1').text.strip
            bean.image_url = coffee_page.css('img data-featured-product-image').attr('src')

            if bean.country_of_origin == "Origin Set" || bean.country_of_origin == "Gift Card (online use only)"
                bean.destroy
            else
                ap bean
            end
    end
end

現在我需要的信息都在頁面上,我正在尋找如下所示的圖像URL,但是對於源頁面上的所有單個coffee_pages。 它需要足夠通用才能拉出這個圖片源,但沒有別的。 我嘗試了許多不同的CSS選擇器,但所有操作都拉為nil或為空白。

<img src="//cdn.shopify.com/s/files/1/2220/0129/products/ceremony-product-gummy-bears_480x480.jpg?v=1551455589" alt="Burundi Kiryama" data-product-featured-image style="display:none">

我在的coffee_page就在這里: https ://shop.ceremonycoffee.com/products/burundi-kiryama

你需要改變

bean.image_url = coffee_page.css('img data-featured-product-image').attr('src')

bean.image_url = coffee_page.css('#mobile-only>img').attr('src')

如果可以,請始終使用附近的標識符來查找要訪問的元素。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM