zoukankan      html  css  js  c++  java
  • 使用JAVA爬取京东商品价格

    有一件物品,你想看看它在京东下所有搜索结果的价格,要怎么办呢?

    京东这个网站还是很好爬的,所有价格信息都写在了Html里面,而且跳到第二页之后,url也是有规律的,基本没有什么技术难度。

    例如:想找ipad这个商品

    在搜索栏输入后显示商品的列表,虽然URL很复杂,但可以简化为:

    http://search.jd.com/Search?keyword=ipad

    换页的话,只要在最后加上一些参数(Get方法):

    http://search.jd.com/Search?keyword=ipad&page=3

    page的数字以奇数方式递增的。

    所以只要连上这个url,再返回html,再解析html就能获得价格和商品名了。

    我是用Jsoup解析的:

    Elements p_name = doc.select("div.p-name");

    Iterator<Element> name = p_name.iterator();

    这里有必要说明下:

    Elements这个类,其实是一个集合,是Jsoup解析html时,获得的所有"div.p-name"的元素的集合

    所有用一个迭代器来遍历。

    效果大概这样:

    Apple iPad mini 2 7.9英寸平板电脑 银色(16G WLAN版/A7芯片/Retina显示屏 ME279CH/A) ¥1988.00 货到付款
    Apple iPad Air 9.7英寸平板电脑 银色(16G WLAN版/A7芯片/Retina显示屏 MD788CH/A) ¥2888.00 货到付款
    Apple iPad mini 2 ME280CH/A (配备 Retina 显示屏 7.9英寸 32G WLAN 机型 银色) ¥2338.00 货到付款
    Apple iPad Air 2 9.7英寸平板电脑 金色(16G WLAN版/A8X 芯片/Retina显示屏/Touch ID技术 MH0W2CH/A) ¥3588.00 货到付款
    Apple iPad Air 9.7英寸平板电脑 银色(32G WLAN版/A7芯片/Retina显示屏 MD789CH/A) ¥3288.00 货到付款
    Apple iPad Air 2 9.7英寸平板电脑 金色(64G WLAN版/A8X 芯片/Retina显示屏/Touch ID技术 MH182CH/A) ¥4288.00 货到付款
    Apple iPad mini 4 7.9英寸平板电脑 金色(16G WLAN版/A8芯片/Retina显示屏/Touch ID技术 MK6L2CH/A) ¥2888.00 货到付款
    Apple iPad Air 2 9.7英寸平板电脑 银色(16G WLAN版/A8X 芯片/Retina显示屏/Touch ID技术 MGLW2CH/A ) ¥3588.00 货到付款
    Apple iPad Air 2 9.7英寸平板电脑 银色(64G WLAN版/A8X 芯片/Retina显示屏/Touch ID技术 MGKM2CH/A ) ¥4288.00 货到付款
    Apple iPad mini 4 7.9英寸平板电脑 金色(64G WLAN版/A8芯片/Retina显示屏/Touch ID技术 MK9J2CH/A) ¥3588.00 货到付款
    Apple iPad mini 2 7.9英寸平板电脑 深空灰色(16G WLAN版/A7芯片/Retina显示屏 ME276CH/A) ¥1988.00 货到付款
    Apple iPad mini 7.9英寸平板电脑 黑色(16G WiFi+Cellular版MD540CH/A) ¥1499.00 货到付款
    Apple iPad mini 2 7.9英寸平板电脑 深空灰色(32G WLAN版/A7芯片/Retina屏 ME277CH/A) ¥2388.00 货到付款
    Apple iPad Air 9.7英寸平板电脑 深空灰色(16G WLAN版/A7芯片/Retina显示屏 MD785CH/A) ¥2888.00 货到付款
    Apple iPad mini 4 7.9英寸平板电脑 银色(16G WLAN版/A8芯片/Retina显示屏/Touch ID技术 MK6K2CH/A) ¥2888.00 货到付款
    Apple iPad Air 2 9.7英寸平板电脑 深空灰色(64G WLAN版/A8X芯片/Retina屏/Touch ID技术 MGKL2CH/A ) ¥4288.00 货到付款
    Apple iPad Air 2 9.7英寸平板电脑 深空灰色(16G WLAN版/A8X芯片/Retina屏/Touch ID技术 MGL12CH/A ) ¥3588.00 货到付款
    Apple iPad Pro 12.9英寸平板电脑 金色(32G WLAN版/A9X芯片/Retina显示屏/Multi-Touch技术 ML0H2CH/A) ¥5888.00 货到付款
    Apple iPad Air 9.7英寸平板电脑 深空灰色(32G WLAN版/A7芯片/Retina显示屏 MD786CH/A) ¥3288.00 货到付款
    Apple iPad Air 2 MH172CH/A (9.7英寸 64G WLAN+Cellular 机型 金色) ¥5188.00 货到付款
    Apple iPad Air 2 9.7英寸平板电脑 金色(128G WLAN版/A8X 芯片/Retina显示屏/Touch ID技术 MH1J2CH/A ) ¥4988.00 货到付款
    Apple iPad mini 2 ME280CH/A (配备 Retina 显示屏 7.9英寸 32G WLAN 银色) ¥2388.00 货到付款
    Apple iPad Pro 12.9英寸平板电脑 金色(128G WLAN版/A9X芯片/Retina显示屏/Multi-Touch技术 ML0R2CH/A) ¥6888.00 货到付款
    Apple iPad mini 4 7.9英寸平板电脑 深空灰色(16G WLAN版/A8芯片/Retina显示屏/Touch ID技术 MK6J2CH/A) ¥2888.00 货到付款
    Apple iPad mini 4 7.9英寸平板电脑 金色(128G WLAN版/A8芯片/Retina显示屏/Touch ID技术 MK9Q2CH/A) ¥4288.00 货到付款
    Apple iPad Air 2 9.7英寸平板电脑 深空灰色(128G WLAN版/A8X芯片/Retina屏/Touch ID技术 MGTX2CH/A) ¥4988.00 货到付款
    Apple iPad Air 2 9.7英寸平板电脑 银色(128G WLAN版/A8X 芯片/Retina显示屏/Touch ID技术 MGTY2CH/A) ¥4988.00 货到付款
    Apple iPad Air 2 MH1C2CH/A (9.7英寸 16G WLAN+Cellular 机型 金色) ¥4488.00 货到付款
    Apple iPad Pro 12.9英寸平板电脑 银色(32G WLAN版/A9X芯片/Retina显示屏/Multi-Touch技术 ML0G2CH/A) ¥5888.00 货到付款
    Apple iPad Pro 12.9英寸平板电脑 银色(128G WLAN版/A9X芯片/Retina显示屏/Multi-Touch技术 ML0Q2CH/A) ¥6888.00 货到付款

    不一定和页面上的顺序对得上,但是名字对应的价格是对的。

    总结一下:

    商品的列表页面:

    http://search.jd.com/Search?keyword=“商品名”&page=“页数”

    真是太简单啦!

  • 相关阅读:
    sql server 中各个系统表的作用==== (转载)
    后台动态设置前台标签内容和属性
    利用C#编写一个简单的抓网页应用程序
    如何创建和使用Web Service代理类
    jdbc如何取得存储过程return返回值
    子窗口和父窗口的函数或对象能否相互访问 (转载)
    把aspx文件编译成DLL文件
    C#中的类型转换
    c#中对文件的操作小结
    转贴一篇 自定义数据库 希望对你有帮助
  • 原文地址:https://www.cnblogs.com/wzben/p/5003845.html
Copyright © 2011-2022 走看看