1

我有这样的链接:

<div class="zg_title">
  <a href="https://rads.stackoverflow.com/amzn/click/com/B000O3GCFU" rel="nofollow noreferrer">Thermos Foogo Leak-Proof Stainless St...</a>     
</div>

我像这样刮他们:

  product_asin = product.xpath('//div[@class="zg_title"]/a/@href').first.value 

问题是它需要整个 URL,我只想获取 ID:

B000O3GCFU

我想我需要做这样的事情:

product_asin = product.xpath('//div[@class="zg_title"]/a/@href').first.value[ReGEX_HERE]

在这种情况下我可以使用的最简单的正则表达式是什么?

编辑:

奇怪的链接 URL 似乎不完整:

http://www.amazon.com/Thermos-Foogo-Leak-Proof-Stainless-10-Ounce/dp/B000O3GCFU/ref=zg_bs_baby-products_1
4

3 回答 3

3

鉴于产品代码始终在/dp/a 之前和之后/

url[/(?<=\/dp\/)[^\/]+/]

或者,也许更具可读性:

url[%r{(?<=/dp/)[^/]+}]

或者,不使用正则表达式:

parts = url.split('/')
parts[parts.index('dp') + 1]
于 2013-08-26T08:18:28.183 回答
3

使用/\w+$/

p doc.xpath('//div[@class="zg_title"]/a/@href').first.value[/\w+$/]

/\w+$/匹配尾随字母、数字、_.


require 'nokogiri'

s = <<EOF
<div class="zg_title">
  <a href="http://rads.stackoverflow.com/amzn/click/B000O3GCFU">Thermos Foogo Leak-Proof Stainless St...</a>     
</div>
EOF

doc = Nokogiri::HTML(s)
p doc.xpath('//div[@class="zg_title"]/a/@href').first.value[/\w+$/]
# => "B000O3GCFU"
于 2013-08-26T07:20:52.153 回答
0

一种基于可用解析器的方法(为了取悦 Nicolas Tyler 或其他任何愿意在这种情况下避免使用正则表达式进行解析的人)

require 'uri'

product_uri = product.xpath('//div[@class="zg_title"]/a/@href').first.value
# e.g. http://www.amazon.com/Thermos-Foogo-Leak-Proof-Stainless-10-Ounce/dp/B000O3GCFU/ref=zg_bs_baby-products_1

product_path = URI.parse( product_asin_uri ).path.split('/')
# => ["", "Thermos-Foogo-Leak-Proof-Stainless-10-Ounce", 
#     "dp", "B000O3GCFU", "ref=zg_bs_baby-products_1"]

# This relies on (un-researched assumption) location in path being consistent
# Now we have components though, we can look at Amazon's documentation and 
# select based on position in path, relative position from some other identifier
# etc, without risk of a regex mismatch

product_asin = product_path[2]
# => "B000O3GCFU"
于 2013-08-26T08:24:31.857 回答