【问题标题】:Crawl with nokogiri can not find the elements用 nokogiri 爬网找不到元素
【发布时间】:2021-12-05 08:43:08
【问题描述】:

我对抓取网页非常陌生。我有一个ali express page 的网址。当我尝试通过以下简单步骤进行抓取时

url = https://www.aliexpress.com/item/4000275547643.html
page = Nokogiri.HTML(open(url))

当我尝试获取产品的详细信息时,它返回 nil

product = page.at(".product-info")#returns nil

谁能帮我解决解析整页需要做的事情。

【问题讨论】:

标签: ruby http nokogiri


【解决方案1】:

你可以这样做

require 'net/http'

# Your main url
url = 'https://www.aliexpress.com/item/4000275547643.html'

# Get the page html
uri = URI.parse(url)
res = Net::HTTP.get_response(uri)

# Scrape the website
doc = Nokogiri::HTML(res.body)
products = doc.css('.product-info')

如果有多个.product-info,这将返回一个数组,您以后可以像这样map

products.map do |product|
    # Do something with the product
end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-02
    • 2011-08-15
    • 1970-01-01
    • 1970-01-01
    • 2020-03-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多