mindpin / TouchIdea

0 stars 1 forks source link

实现 infocard 解析服务 #22

Closed fushang318 closed 9 years ago

fushang318 commented 9 years ago

infocard-service

写一个 rails 工程,用来解析收集 infocard

版本库地址 https://github.com/mindpin/infocard-service 需求详情 https://github.com/mindpin/TouchIdea/issues/19

infocard 存储模型

infocard
  url
  hostname
  homepage
  image_url
  title
  desc
  price
  location

解析实现的使用方式如下

url = "http://detail.tmall.com/item.htm?id=15593325475&spm=a1z0k.7385961.1997985097.d4918997.lTDqPh&_u=rkkbfj13db&mt="

InfocardItem
 infocard_item = InfocardParser.parse(url)

# 要解析的网页
infocard_item.url

# 网站名称
infocard_item.hostname

# 网站首页地址
infocard_item.homepage

# 图片
infocard_item.image_url

# 商品名称
infocard_item.title

# 商品描述
infocard_item.desc

# 定价
infocard_item.price

# 地理位置
infocard_item.location

下面以实现天猫的商品页解析类举例

url = "http://detail.tmall.com/item.htm?id=15593325475&spm=a1z0k.7385961.1997985097.d4918997.lTDqPh&_u=rkkbfj13db&mt="
u = URI.parse(url)

# 实现这个类方法
TmallInfocardParser.can_parse?(u.host)

# 实现这个类方法
infocard_item = TmallInfocardParser.parse(url)

# 实现以下几个解析信息方法
tmall_infocard_parser.get_url

tmall_infocard_parser.get_hostname

tmall_infocard_parser.get_homepage

tmall_infocard_parser.get_image_url

tmall_infocard_parser.get_title

tmall_infocard_parser.get_desc

tmall_infocard_parser.get_price

tmall_infocard_parser.get_location

所有解析类都要实现 TmallInfocardParser 实现的这些方法

大概的一些实现代码

class InfocardParser
  def self.parse(url)
    u = URI.parse(url)
    case true
    when TmallInfocardParser.can_parse?(u.host)
      return TmallInfocardParser.parse(url)
    when ....
      ...
    when ...
      ...
    end
  end
end

# 解析类里可以被所有解析类公用的代码,抽取到这个 module 
module InfocardParserMethods
  ........
end

class TmallInfocardParser
  inlcude InfocardParserMethods

  def self.parse(url)
    parser = self.new(url)
    item = InfocardItem.new
    item.hostname = parser.get_hostname
    ...........
    return item
  end

  # 实现其他方法
  ............

end

工作量最大的事情就是,实现各个解析类的 get_hostname get_homepage 这些方法

fushang318 commented 9 years ago

没有问题的

京东 亚马逊(中国) 凡客 当当 1号店 AliceInMod 易迅 宜家 趣玩 好乐买 NOP 薄荷糯米葱 国美在线 优集品 丽芙家居 无印良品(中国) YOHO!有货 新蛋中国 欧舒丹中国 凡夫


天猫

缺少价格获取,这个有思路了,但是实现比较繁琐,先做其他的

天猫多种情况实例 http://markdown.4ye.me/rRYAVhDb

ben7th commented 9 years ago

目前已经实现了测试页面,测试页面要求见: https://github.com/mindpin/TouchIdea/issues/26

测试页面访问地址见: https://banana-cake-6494.herokuapp.com/

ben7th commented 9 years ago

4 月 9 日调整建议

https://github.com/mindpin/infocard-service 工程建议改名叫: urlinfo-service

同时提供 api 的网址建议改为 urlinfo.4ye.me

针对图片信息,在测试页面上同时显示图片 url 和 这样比较便于检查


  1. 工程改名,部署地址改名
  2. 删除测试页上的多余信息
  3. 把支持的网站列表的每行改成三个链接:

    <a href = 'http://www.tmall.com/'>天猫</a>
    <a href = '具体商品页链接'>示例商品页</a>
    <a href = 'http://markdown.4ye.me/z1eiy6c'>http://markdown.4ye.me/z1eiy6c<a>
  4. 目前解析出来的图片 url,在解析结果上同时显示 url 字符串 和
  5. 根据楼下的 JSON 格式的三种情况,对程序进行调整
  6. 第二种情况“不在支持列表里的网站的SEO信息获取”细化考虑一下。
  7. 程序结构改进

    改成支持只传 block 的形式

ben7th commented 9 years ago

目前的 JSON 格式设计: https://github.com/mindpin/infocard-service/issues/1

需要修改为

支持的网站,获取特定信息

url 
  /api/fetch_infocard
method
  get
params
  url
response 
  code 200
  body 格式
  {
    :status => "PARSED",
    :data   =>   {
      image_urls: [
        "",
        "",
        ""
      ],
      title: "",
      price: ""
    }
  }

不支持的网站,获取 seo 信息

url 
  /api/fetch_infocard
method
  get
params
  url
response 
  code 200
  body 格式
  {
    :status => "SEO",
    :data   =>   {
      title: "",
      meta: "",
      ....
    }
  }

解析错误的网站

url 
  /api/fetch_infocard
method
  get
params
  url
response 
  code 500
  body 格式
  {
    :status => "error"
  }
ben7th commented 9 years ago

需要准备一个表格,来记录目前 urlinfo.4ye.me 已经支持的网站解析列以及完成情况

表格参考以下格式整理。需要填写的内容是:

  1. 网站名,网站链接,以及示例商品网页链接;
  2. 备注说明,需要说明的内容包括:现在是通过什么解析的(解析HTML,解析网页包含的js,使用网站提供的API等),有什么遗留问题(某些信息取不到,图片不能外链,有时会返回403等错误码等)
淘宝 http://www.taobao.com/ http://item.taobao.com/item.htm?spm=a217p.7287761.1998186321-0.1.5md6cI&id=39659481806&_scm=1029.list.htm.item.1
通过解析网页HTML进行支持
豆瓣电影 http://movie.douban.com/ http://movie.douban.com/subject/23761370/?from=showing
通过豆瓣 API 进行解析;图片不能外链
arlyxiao commented 9 years ago
天猫

http://www.tmall.com/

http://detail.tmall.com/item.htm?id=15593325475&spm=a1z0k.7385961.1997985097.d4918997.lTDqPh&_u=rkkbfj13db&mt=
通过解析网页HTML进行支持, 价格通过另一链接获取 http://mdskip.taobao.com/core/initItemDetail.htm?itemId=#{pid} , 天猫多种情况实例 http://markdown.4ye.me/rRYAVhDb
京东

http://www.jd.com/

http://item.jd.com/1082403.html
通过解析网页HTML进行支持, 价格通过另一链接获取 http://p.3.cn/prices/get?skuid=J_#{pid}&type=1&area=5_142_143&callback=cnp
亚马逊(中国)

http://www.amazon.cn/

http://www.amazon.cn/gp/product/B007ZY5GV2/ref=s9_simh_gw_p79_d14_i2_gs9w?pf_rd_m=A1AJ19PSB66TGU&pf_rd_s=center-2&pf_rd_r=0072J7PVAHGXQ2KXBTNA&pf_rd_t=101&pf_rd_p=248108232&pf_rd_i=899254051
通过解析网页HTML进行支持,
凡客

http://www.vancl.com/

http://item.vancl.com/1684304.html#ref=zt_zhenzhiku-9229-0-0-v|hp-hp-1-r_4-v:n
通过解析网页HTML进行支持,
当当

http://www.dangdang.com/

http://product.dangdang.com/22788959.html#ddclick_reco_book
通过解析网页HTML进行支持,
1号店

http://www.yhd.com/

http://item.yhd.com/item/1884193?tp=2025.0.983.0.8.KlQWfFA-10-D5lua
通过解析网页HTML进行支持, 价格通过另一链接获取 http://gps.yhd.com/restful/detail?mcsite=1&provinceId=4&pmId=#{pid}&callback=jsonp
AliceInMod

http://www.aliceinmod.com/

http://www.aliceinmod.com/?product-5621.html
通过解析网页HTML进行支持,
易迅

http://www.yixun.com/

http://item.yixun.com/item-1918368.html?YTAG=1.100031206&cp-ptss=1815-3-1918368
通过解析网页HTML进行支持,
宜家

http://www.ikea.com/cn/zh/

http://www.ikea.com/cn/zh/catalog/products/40281630/
通过解析网页HTML进行支持,
趣玩

http://www.quwan.com/

http://www.quwan.com/goods_2013-42938.html?FM=lastlike
通过解析网页HTML进行支持,
好乐买

http://www.okbuy.com/

http://www.okbuy.com/p-anta/detail-shoe-17116591.html
通过解析网页HTML进行支持, 价格通过另一页面获取 http://www.okbuy.com/ajax/product/info/#{pid}
薄荷糯米葱

http://www.brandnewchina.cn/

http://www.brandnewchina.cn/getProduct.htm?productid=18722&color=RED
通过解析网页HTML进行支持,
NOP

http://www.nop.cn/

http://www.nop.cn/product-2737.html
通过解析网页HTML进行支持,
优集品

http://www.ujipin.com/

http://www.ujipin.com/g-i15937.html?track=home-time-p2
通过解析网页HTML进行支持,
国美在线

http://www.gome.com.cn/

http://www.gome.com.cn/product/9133498332.html
通过解析网页HTML进行支持,
丽芙家居

http://www.lifevc.com/

http://www.lifevc.com/item/11875#ref_src=gssv
通过解析网页HTML进行支持,
无印良品(中国)

http://www.muji.com.cn/

http://www.muji.com.cn/cn/store/goods/4548076448341
通过解析网页HTML进行支持, 价格通过解析页面上的 script json 获取
YOHO!有货

http://www.yohobuy.com/

http://item.yohobuy.com/product/pro_40359_62325/HeiTouYuMaoXiangLian.html
通过解析网页HTML进行支持,
新蛋中国

http://www.newegg.cn/

http://www.newegg.cn/Product/A6D-2VX-05H.htm?neg_sp=Home-_-A6D-2VX-05H-_-YouLike
通过解析网页HTML进行支持,
欧舒丹中国

http://www.loccitane.cn/

http://www.loccitane.cn/cherry-blossom-solid-perfume,90,2,34817,309403.htm#s=34818
通过解析网页HTML进行支持,
凡夫

http://www.itruelife.com/

http://www.itruelife.com/product/300101003.html
通过解析网页HTML进行支持,
兰蔻(中国)

http://www.lancome.com.cn/lancome/_zh/_cn/index.aspx

http://www.lancome.com.cn/lancome/_zh/_cn/lancome/skincare/the-essence-of/genifique-ls00081.aspx
通过解析网页HTML进行支持,
KnewOne

https://knewone.com/

https://knewone.com/things/vojo-neng-liang-cha-tou
通过解析网页HTML进行支持,
理肤泉

http://www.larocheposay.com.cn/

http://www.larocheposay.com.cn/store/chanpinxilie/shuhuanpenwu/lifuquanshuhuanpenwu300ml.html
通过解析网页HTML进行支持,
良仓

http://www.iliangcang.com/

http://www.iliangcang.com/i/goods/?id=34752
通过解析网页HTML进行支持,
唯品会

http://www.vip.com/

http://www.vip.com/detail-383475-50536007.html?f=0|0|0|1
通过解析网页HTML进行支持,
Amazon

http://www.amazon.com/

http://www.amazon.com/gp/product/B00P1SYWLY/ref=s9_pop_gw_g121_i5?pf_rd_m=ATVPDKIKX0DER&pf_rd_s=desktop-2&pf_rd_r=1A1GZDJ2R6ZMMT9KTNJX&pf_rd_t=36701&pf_rd_p=1970555362&pf_rd_i=desktop
通过解析网页HTML进行支持,
Fab

http://www.fab.com/

http://fab.com/product/virgo-necklace-turquoise-493741/?pref[]=designer%7Ccarla-de-la-cruz-jewelry&ref=designer&pos=3
通过解析网页HTML进行支持,
ebay

http://www.ebay.com/

http://www.ebay.com/itm/Radio-Flow-Lexon-Bleu-/251634701185?&_trksid=p2056016.l4276
通过解析网页HTML进行支持,
大众点评网

http://www.dianping.com/

http://www.dianping.com/shop/15861615
通过解析网页HTML进行支持,
Yelp

http://www.yelp.com/

http://www.yelp.com/biz/sous-beurre-kitchen-san-francisco-2
通过解析网页HTML进行支持,
豆瓣阅读

http://read.douban.com/

http://read.douban.com/ebook/8305815/?icn=index-gallery
通过解析网页HTML进行支持,
豆瓣读书

http://book.douban.com

http://book.douban.com/subject/25862578/
通过豆瓣 API 进行解析, 图片可以外链
多看阅读

http://www.duokan.com/

http://www.duokan.com/book/42790
通过解析网页HTML进行支持,
豆瓣电影

http://movie.douban.com/

http://movie.douban.com/subject/6875263/
通过豆瓣 API 进行解析;图片不能外链
时光网

http://www.mtime.com/

http://movie.mtime.com/204906/
通过解析网页HTML进行支持,
Imdb

http://www.imdb.com/

http://www.imdb.com/title/tt1661199/?ref_=rvi_tt
通过解析网页HTML进行支持,
安卓市场

http://apk.hiapk.com/

http://apk.hiapk.com/appinfo/com.tencent.mm
通过解析网页HTML进行支持,
安智市场

http://www.anzhi.com/

http://www.anzhi.com/soft_2122818.html
通过解析网页HTML进行支持,
应用汇

http://www.appchina.com/

http://www.appchina.com/app/com.linekong.cjad.yyh
通过解析网页HTML进行支持,
豌豆荚

http://www.wandoujia.com/

http://www.wandoujia.com/apps/com.gewara
通过解析网页HTML进行支持,
机锋市场

http://apk.gfan.com/

http://apk.gfan.com/Product/App721206.html
通过解析网页HTML进行支持,
应用宝

http://android.myapp.com/

http://android.myapp.com/myapp/detail.htm?apkName=com.vanchu.apps.guimiquan
通过解析网页HTML进行支持,
App Store(中国)

http://itunes.apple.com/cn/genre/ios/id36?mt=8

https://itunes.apple.com/cn/app/bad-piggies/id533451786?mt=8
通过解析网页HTML进行支持,
iTunes Store - 播客(中国)

https://itunes.apple.com/cn/genre/bo-ke/id26?mt=2

https://itunes.apple.com/cn/podcast/wei-zhi-tian-xia/id398155040?mt=2
通过解析网页HTML进行支持,
iTunes Store - 图书(中国)

https://itunes.apple.com/cn/genre/tu-shu/id38?mt=11

https://itunes.apple.com/cn/book/xi-xiang-ji/id894592940?mt=11
通过解析网页HTML进行支持,
豆瓣移动应用

http://www.douban.com/

http://www.douban.com/subject/25890156/
通过解析网页HTML进行支持,
淘宝

http://www.taobao.com/

http://item.taobao.com/item.htm?spm=a219r.lm5179.0.0.yViRpx&id=43782883043&ns=1&abbucket=17
通过解析网页HTML进行支持,
arlyxiao commented 9 years ago

1, 京东API目前在代码上已经可以获取 access_token,具体代码请参考

https://gitcafe.com/arlyxiao/im-chat-parser-demo

实现过程中一些可以分享的细节 1, oauth 验证流程,参考文档 http://help.jd.com/jos/question-594.html

2, 如何获取商品ID

需要解析 HTML,找到 venderId 字眼分析获取 参考页面 http://item.jd.com/1522032740.html

3, 如何通过 API + 商品ID 获取商品资料

1, 根据API,获取商品信息,参考文档 http://jos.jd.com/api/detail.htm?apiName=360buy.ware.get&id=108

2, api 调用一些注意点,特别是 sign 生成,参考文档 http://help.jd.com/jos/question-580.html#A6

目前依然无法通过 API 获取到商品资料的原因在于, 需要注册成为商家账号

请参考: http://jos.jd.com/faq/detail.htm?id=142

4, 总结: 还需要解决的问题

1, 由于前台是人工浏览器手动登录 需要实践 refreshtoken 在后台自动运行的情况 2, sign 字符串的生成总是有些问题 3, 商家注册, 需要企业营业职照,税务等信息,请参考 http://i.jcloud.com/user/info/certified

arlyxiao commented 9 years ago

1, Taobao API 目前通过申请到的 APP KEY,可直接使用 Gem https://github.com/wongyouth/open_taobao (该部分已由宋亮尝试过)

2, 没能找到有 API 提供促销价, 主要尝试以下几种途径

1, 在淘宝商品 API 分类目录下根据API名对几类比较有可能有提供价格的进行试验 taobao.products.search 搜索产品信息: http://open.taobao.com/apidoc/api.htm?spm=a219a.7386789.1998342952.2.vLuuBR&path=cid:4-apiId:5

taobao.item.get 得到单个商品信息 http://open.taobao.com/apidoc/api.htm?spm=a219a.7386789.1998342952.8.vLuuBR&path=cid:4-apiId:20

taobao.items.custom.get http://open.taobao.com/apidoc/api.htm?path=cid:4-apiId:163

taobao.items.seller.list.get 批量获取商品详细信息 http://open.taobao.com/apidoc/api.htm?path=cid:4-apiId:24626

2, 搜索 promotion 字眼, http://open.taobao.com//search/searcher.htm?t=api&q=promotion , 或者有 promotion 字眼相关的 API taobao.ump.promotion.get 商品优惠详情查询 http://open.taobao.com/apidoc/api.htm?spm=a219a.7386789.1998342952.38.sTOwPJ&path=cid:4-apiId:11039

3, 发送邮箱咨询: 其他咨询 top#taobao.com

arlyxiao commented 9 years ago

目前没有找到 amazon 有直接提供可以获取商品资料的 API, 但有一个类似的接近的 API, Product Advertising API http://docs.aws.amazon.com/AWSECommerceService/latest/DG/ItemLookup.html

Product Advertising API 只能获取到 title, 并且是通过 Asin 值来获取

什么是 Asin (item_id), 中文版说明 https://associates.amazon.cn/gp/associates/help/t5/a16?pf_rd_m=A1AJ19PSB66TGU&pf_rd_s=assoc-left-1&pf_rd_r=&pf_rd_t=501&pf_rd_p=&pf_rd_i=assoc_help_main

下面简单介绍下如何使用 Product Advertising API

1, 前期准备

需要 access key, secret key, associate_tag 才能使用 Amazon Product Advertising API

1, 通过 http://aws.amazon.com/ 申请一个账号, 获取 access key 跟 secret key

2, 通过 https://affiliate-program.amazon.com 获取 associate_tag 值

2, 可直接使用的 Gem

https://github.com/jugend/amazon-ecs

ben7th commented 9 years ago

已经集成到 0.2