爬虫——简单处理js中嵌入的json数据
看了群里一个人提问道https://www.amazon.com/,商品分类那里无法用xpath拿得到列表。遂对其研究。
通过抓包工具可以得知,原始数据存在于js代码中,我的方式是手动解析js,从里面抠出来json数据获取
这是数据包:
window.$Nav && $Nav.when("data").run(function(data) { data({"wishlistContent":{"template":{"name":"itemList","data":{"items":[{"text":"创建一个心愿单","url":"/gp/registry/wishlist?ie=UTF8&ref_=nav_wishlist_create&triggerElementID=createList"},{"text":"查找其他人的心愿单","url":"/gp/registry/search?ie=UTF8&ref_=nav_wishlist_find"},{"text":"查找礼品","url":"/gcx/Gifts-for-Adults/gfhz/?_encoding=UTF8&ref_=nav_wishlist_gf"},{"subtext":"安装亚马逊助理","text":"保存Web中的商品","url":"/gp/BIT?ie=UTF8&bitCampaignCode=a0032&ref_=nav_wishlist_bit_v2_a0032"},{"text":"婚庆礼品登记清单","url":"/wedding/home?_encoding=UTF8&ref_=nav_wishlist_wr"},{"text":"婴儿礼品登记清单","url":"/baby-reg/homepage?_encoding=UTF8&ref_=nav_wishlist_br"},{"text":"亲友馈赠","url":"/gp/gift-central/organizer?ie=UTF8&ref_=nav_wishlist_fafgift"},{"text":"AmazonSmile慈善列表","url":"/gp/clpf?ie=UTF8&ref_=nav_wishlist_smi_se_ya_lll_ll"},{"text":"Pantry 清单","url":"/gp/pantry/yourlists?ie=UTF8&ref_=nav_wishlist_pntry_gno"},{"text":"我的中心","url":"/stream/saves/items?_encoding=UTF8&ref_=nav_wishlist_strm_in_youtique_lists"},{"text":"探索创意列表","url":"/ideas?_encoding=UTF8&ref_=nav_wishlist_idea_nav"},{"text":"探索展室","url":"/showroom?_encoding=UTF8&ref_=nav_wishlist_srm_your_desk_wl"},{"text":"参加家庭风格测验","url":"/stylequiz/?_encoding=UTF8&ref_=nav_wishlist_sq_ya_yl"},{}]}},"wlTriggers":"57042:165723:180559:159521"},"yourAccountContent":{"template":{"name":"itemList","data":{"items":[{"text":"我的账户","url":"/gp/css/homepage.html?ie=UTF8&ref_=nav_youraccount_ya"},{"text":"我的订单","url":"/gp/css/order-history?ie=UTF8&ref_=nav_youraccount_orders","id":"nav_prefetch_yourorders"},{"text":"我的心愿单","url":"/gp/registry/wishlist?ie=UTF8&ref_=nav_youraccount_wl&requiresSignIn=1"},{"text":"为我推荐","url":"/gp/yourstore?ie=UTF8&ref_=nav_youraccount_recs"},{"text":"我的”订购与节省“商品","url":"/gp/subscribe-and-save/manager/viewsubscriptions?ie=UTF8&ref_=nav_youraccount_sns"},{"text":"成员资格和订阅","url":"/yourmembershipsandsubscriptions?_encoding=UTF8&ref_=nav_youraccount_digital_subscriptions"},{"text":"我的服务请求","url":"/localservices/ya/servicerequests?_encoding=UTF8&ref_=nav_youraccount_desktop_vas_requestlist"},{"text":"我的车库","url":"/gp/your-garage/?ie=UTF8&ref_=nav_youraccount_au_pf_as_GNO"},{"text":"您的运动粉丝商店","url":"/fanshop?_encoding=UTF8&ref_=nav_youraccount_yfs"},{"text":"我的宠物","url":"/yourpets?_encoding=UTF8&ref_=nav_youraccount_pet_profiles"},{"text":"管理我的内容和设备","url":"/mn/dcw/myx.html?_encoding=UTF8&ref_=nav_youraccount_myk"},{"text":"我的音乐库","url":"/gp/dmusic/mp3/player?ie=UTF8&ref_=nav_youraccount_cldplyr"},{"text":"我的Amazon Drive","url":"/clouddrive?_encoding=UTF8&ref_=nav_youraccount_clddrv"},{"text":"Prime 即时录像","url":"/Prime-Instant-Video/s/browse?_encoding=UTF8&node=2676882011&ref_=nav_youraccount_piv"},{"text":"我的 Kindle Unlimited","url":"/gp/kindle/ku/ku_central?ie=UTF8&ref_=nav_youraccount_ku"},{"text":"我的播放列表","url":"/gp/video/watchlist?ie=UTF8&ref_=nav_youraccount_ywl"},{"text":"我的录像库","url":"/gp/video/library?ie=UTF8&ref_=nav_youraccount_yvl"},{"text":"我的应用程序和设备","url":"/gp/mas/your-account/myapps?ie=UTF8&ref_=nav_youraccount_aad"},{}]}},"signInHtml":"<div id='nav-flyout-ya-signin' class='nav-flyout-content'><a href='/gp/navigation/redirector.html/ref=sign-in-redirect?ie=UTF8&associationHandle=usflex&currentPageURL=https%3A%2F%2Fwww.amazon.com%2Fgp%2Fyourstore%2Fhome%3Fie%3DUTF8%26ref_%3Dnav_signin&pageType=&switchAccount=&yshURL=https%3A%2F%2Fwww.amazon.com%2Fgp%2Fyourstore%2Fhome%3Fie%3DUTF8%26ref_%3Dnav_signin' rel='nofollow' class='nav-action-button' data-nav-role='signin' data-nav-ref='nav_signin'><span class='nav-action-inner'>立即登录</span></a><div id='nav-flyout-ya-newCust' class='nav_pop_new_cust nav-flyout-content'>新客户? <a href='https://www.amazon.com/ap/register?_encoding=UTF8&openid.assoc_handle=usflex&openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.mode=checkid_setup&openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&openid.ns.pape=http%3A%2F%2Fspecs.openid.net%2Fextensions%2Fpape%2F1.0&openid.pape.max_auth_age=0&openid.return_to=https%3A%2F%2Fwww.amazon.com%2Fgp%2Fyourstore%2Fhome%3Fie%3DUTF8%26ref_%3Dnav_newcust' rel='nofollow' class='nav-a'>免费注册</a></div></div>","wlTriggers":"205695:159521"},"shopAllContent":{"template":{"name":"itemList","data":{"items":[{"text":"Prime视频","url":"/s/browse?_encoding=UTF8&node=2858778011&ref_=nav_shopall-export_nav_mw_sbd_intl_video"},{"text":"音乐、CD和乙烯基塑料唱片","url":"/s/browse?_encoding=UTF8&node=5174&ref_=nav_shopall-export_nav_mw_sbd_intl_cd"},{"text":"数字音乐","url":"/s/browse?_encoding=UTF8&node=163856011&ref_=nav_shopall-export_nav_mw_sbd_intl_music"},{"text":"Kindle 商店","url":"/s/browse?_encoding=UTF8&node=133140011&ref_=nav_shopall-export_nav_mw_sbd_intl_kindle"},{"text":"工艺品","url":"/s/browse?_encoding=UTF8&node=4954955011&ref_=nav_shopall-export_nav_mw_sbd_intl_arts"},{"text":"汽车","url":"/s/browse?_encoding=UTF8&node=2562090011&ref_=nav_shopall-export_nav_mw_sbd_intl_automotive"},{"text":"婴儿用品","url":"/s/browse?_encoding=UTF8&node=16225005011&ref_=nav_shopall-export_nav_mw_sbd_intl_baby"},{"text":"美容和个人护理","url":"/s/browse?_encoding=UTF8&node=16225006011&ref_=nav_shopall-export_nav_mw_sbd_intl_beauty"},{"text":"图书","url":"/s/browse?_encoding=UTF8&node=283155&ref_=nav_shopall-export_nav_mw_sbd_intl_books"},{"text":"计算机","url":"/s/browse?_encoding=UTF8&node=16225007011&ref_=nav_shopall-export_nav_mw_sbd_intl_computers"},{"text":"电子产品","url":"/s/browse?_encoding=UTF8&node=16225009011&ref_=nav_shopall-export_nav_mw_sbd_intl_electronics"},{"text":"女士时尚","url":"/s/browse?_encoding=UTF8&node=16225018011&ref_=nav_shopall-export_nav_mw_sbd_intl_womenfasion"},{"text":"男士时尚","url":"/s/browse?_encoding=UTF8&node=16225019011&ref_=nav_shopall-export_nav_mw_sbd_intl_menfasion"},{"text":"女童时尚","url":"/s/browse?_encoding=UTF8&node=16225020011&ref_=nav_shopall-export_nav_mw_sbd_intl_girlfasion"},{"text":"男童时尚","url":"/s/browse?_encoding=UTF8&node=16225021011&ref_=nav_shopall-export_nav_mw_sbd_intl_boyfasion"},{"text":"健康和家居用品","url":"/s/browse?_encoding=UTF8&node=16225010011&ref_=nav_shopall-export_nav_mw_sbd_intl_health"},{"text":"家居厨房用品","url":"/s/browse?_encoding=UTF8&node=16225011011&ref_=nav_shopall-export_nav_mw_sbd_intl_kitchen"},{"text":"工业科技","url":"/s/browse?_encoding=UTF8&node=16225012011&ref_=nav_shopall-export_nav_mw_sbd_intl_industrial"},{"text":"行李箱包","url":"/s/browse?_encoding=UTF8&node=16225017011&ref_=nav_shopall-export_nav_mw_sbd_intl_luggage"},{"text":"电影和电视","url":"/s/browse?_encoding=UTF8&node=2625373011&ref_=nav_shopall-export_nav_mw_sbd_intl_movies"},{"text":"宠物用品","url":"/s/browse?_encoding=UTF8&node=16225013011&ref_=nav_shopall-export_nav_mw_sbd_intl_pet"},{"text":"软件","url":"/s/browse?_encoding=UTF8&node=16225008011&ref_=nav_shopall-export_nav_mw_sbd_intl_software"},{"text":"运动与户外用品","url":"/s/browse?_encoding=UTF8&node=16225014011&ref_=nav_shopall-export_nav_mw_sbd_intl_sports"},{"text":"工具与家居装饰","url":"/s/browse?_encoding=UTF8&node=256643011&ref_=nav_shopall-export_nav_mw_sbd_intl_tools"},{"text":"玩具与游戏","url":"/s/browse?_encoding=UTF8&node=16225015011&ref_=nav_shopall-export_nav_mw_sbd_intl_toys"},{"text":"视频游戏","url":"/s/browse?_encoding=UTF8&node=16225016011&ref_=nav_shopall-export_nav_mw_sbd_intl_video_games"},{"text":"销售和优惠","url":"/s/browse?_encoding=UTF8&node=15529609011&ref_=nav_shopall-export_nav_mw_sbd_intl_sales"}]}}},"cartContent":{"html":"<div id='nav-cart-flyout' class='nav-empty nav-flyout-content' data-one='' data-many=''><div class='nav-dynamic-full'><div id='nav-cart-standard' class='nav-cart-content'><a href='/gp/cart/view.html/ref=nav_flyout_viewcart?ie=UTF8&hasWorkingJavascript=1' class='nav-cart-title'>您购物车中的商品</a><div class='nav-cart-subtitle'></div><div class='nav-cart-items'></div></div><div id='nav-cart-pantry' class='nav-cart-content' data-box='' data-boxes='' data-box-filled='' data-boxes-filled=''><a href='/gp/cart/view.html/ref=nav_flyout_viewcart?ie=UTF8&hasWorkingJavascript=1' class='nav-cart-title'></a><div class='nav-cart-subtitle'></div><div class='nav-cart-items'></div></div><div id='nav-cart-fresh' class='nav-cart-content'><a href='/gp/cart/view.html/ref=nav_flyout_viewcart?ie=UTF8&hasWorkingJavascript=1' class='nav-cart-title'><img id='nav-cart-fresh-logo' src='https://images-na.ssl-images-amazon.com/images/G/01/gno/ec-logo-fresh-color._CB305770688_.png'></a><div class='nav-cart-subtitle'></div><div class='nav-cart-items'></div></div></div><div class='nav-ajax-message'></div><div class='nav-dynamic-empty'><p class='nav_p nav-bold nav-cart-empty'> 您的购物车是空的。</p><p class='nav_p '> 挑选您喜爱的各种商品并放入购物车吧!</p><p class='nav_p '> 如果您已经有了账户,请<a href='/gp/navigation/redirector.html/ref=sign-in-redirect?ie=UTF8&associationHandle=usflex¤tPageURL=https%3A%2F%2Fwww.amazon.com%2Fgp%2Fyourstore%2Fhome%3Fie%3DUTF8%26ref_%3Dnav_signin_cart&pageType=&switchAccount=&yshURL=https%3A%2F%2Fwww.amazon.com%2Fgp%2Fyourstore%2Fhome%3Fie%3DUTF8%26ref_%3Dnav_signin_cart' class='nav_a'>登录</a>。</p></div><div class='nav-ajax-error-msg'><p class='nav_p nav-bold'> 现在预览您的购物车存在问题。</p><p class='nav_p '> 检查您的互联网设置并<a href='/gp/cart/view.html/ref=nav_flyout_viewcart?ie=UTF8&hasWorkingJavascript=1' class='nav_a'>转到您的购物车</a>,或者<a href='javascript:void(0);' class='nav_a nav-try-again'>重试</a>。</p></div><div id='nav-cart-footer'><a href='/gp/cart/view.html/ref=nav_flyout_viewcart?ie=UTF8&hasWorkingJavascript=1' id='nav-cart-menu-button' class='nav-action-button'><span class='nav-action-inner'>查看购物车<span id='nav-cart-menu-button-count' ><span id='nav-cart-zero'>(<span class='nav-cart-count'>0</span>件商品)</span><span id='nav-cart-one' style='display: none;'>(<span class='nav-cart-count'>0</span>件商品)</span><span id='nav-cart-many' style='display: none;'>(<span class='nav-cart-count'>0</span>件商品)</span></span></span></a></div></div>"},"signinContent":{"html":"<div id='nav-signin-tooltip'><a href='/gp/navigation/redirector.html/ref=sign-in-redirect?ie=UTF8&associationHandle=usflex&currentPageURL=https%3A%2F%2Fwww.amazon.com%2Fgp%2Fyourstore%2Fhome%3Fie%3DUTF8%26ref_%3Dnav_custrec_signin&pageType=&switchAccount=&yshURL=https%3A%2F%2Fwww.amazon.com%2Fgp%2Fyourstore%2Fhome%3Fie%3DUTF8%26ref_%3Dnav_custrec_signin' class='nav-action-button' data-nav-role='signin' data-nav-ref='nav_custrec_signin'><span class='nav-action-inner'>立即登录</span></a><div class='nav-signin-tooltip-footer'>新客户? <a href='https://www.amazon.com/ap/register?_encoding=UTF8&openid.assoc_handle=usflex&openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.mode=checkid_setup&openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&openid.ns.pape=http%3A%2F%2Fspecs.openid.net%2Fextensions%2Fpape%2F1.0&openid.pape.max_auth_age=0&openid.return_to=https%3A%2F%2Fwww.amazon.com%2Fgp%2Fyourstore%2Fhome%3Fie%3DUTF8%26ref_%3Dnav_custrec_newcust' class='nav-a'>免费注册</a></div></div>"},"accountListContent":{"html":"<div id='nav-al-container'><div id='nav-al-signin'><div id='nav-flyout-ya-signin' class='nav-flyout-content'><a href='/gp/navigation/redirector.html/ref=sign-in-redirect?ie=UTF8&associationHandle=usflex&currentPageURL=https%3A%2F%2Fwww.amazon.com%2Fgp%2Fyourstore%2Fhome%3Fie%3DUTF8%26ref_%3Dnav_signin&pageType=&switchAccount=&yshURL=https%3A%2F%2Fwww.amazon.com%2Fgp%2Fyourstore%2Fhome%3Fie%3DUTF8%26ref_%3Dnav_signin' rel='nofollow' class='nav-action-button' data-nav-role='signin' data-nav-ref='nav_signin'><span class='nav-action-inner'>立即登录</span></a><div id='nav-flyout-ya-newCust' class='nav_pop_new_cust nav-flyout-content'>新客户? <a href='https://www.amazon.com/ap/register?_encoding=UTF8&openid.assoc_handle=usflex&openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.mode=checkid_setup&openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&openid.ns.pape=http%3A%2F%2Fspecs.openid.net%2Fextensions%2Fpape%2F1.0&openid.pape.max_auth_age=0&openid.return_to=https%3A%2F%2Fwww.amazon.com%2Fgp%2Fyourstore%2Fhome%3Fie%3DUTF8%26ref_%3Dnav_newcust' rel='nofollow' class='nav-a'>免费注册</a></div></div></div><div id='nav-al-wishlist' class='nav-al-column nav-tpl-itemList'><div class='nav-title' id='nav-al-title'>我的心愿单</div><a href='/gp/registry/wishlist?ie=UTF8&ref_=nav_wishlist_create&triggerElementID=createList' class='nav-link nav-item'><span class='nav-text'>创建一个心愿单</span></a><a href='/gp/registry/search?ie=UTF8&ref_=nav_wishlist_find' class='nav-link nav-item'><span class='nav-text'>查找其他人的心愿单</span></a><a href='/gcx/Gifts-for-Adults/gfhz/?_encoding=UTF8&ref_=nav_wishlist_gf' class='nav-link nav-item'><span class='nav-text'>查找礼品</span></a><a href='/gp/BIT?ie=UTF8&bitCampaignCode=a0032&ref_=nav_wishlist_bit_v2_a0032' class='nav-link nav-item'><span class='nav-text'>保存Web中的商品</span></a><a href='/wedding/home?_encoding=UTF8&ref_=nav_wishlist_wr' class='nav-link nav-item'><span class='nav-text'>婚庆礼品登记清单</span></a><a href='/baby-reg/homepage?_encoding=UTF8&ref_=nav_wishlist_br' class='nav-link nav-item'><span class='nav-text'>婴儿礼品登记清单</span></a><a href='/gp/gift-central/organizer?ie=UTF8&ref_=nav_wishlist_fafgift' class='nav-link nav-item'><span class='nav-text'>亲友馈赠</span></a><a href='/gp/clpf?ie=UTF8&ref_=nav_wishlist_smi_se_ya_lll_ll' class='nav-link nav-item'><span class='nav-text'>AmazonSmile慈善列表</span></a><a href='/gp/pantry/yourlists?ie=UTF8&ref_=nav_wishlist_pntry_gno' class='nav-link nav-item'><span class='nav-text'>Pantry 清单</span></a><a href='/stream/saves/items?_encoding=UTF8&ref_=nav_wishlist_strm_in_youtique_lists' class='nav-link nav-item'><span class='nav-text'>我的中心</span></a><a href='/ideas?_encoding=UTF8&ref_=nav_wishlist_idea_nav' class='nav-link nav-item'><span class='nav-text'>探索创意列表</span></a><a href='/showroom?_encoding=UTF8&ref_=nav_wishlist_srm_your_desk_wl' class='nav-link nav-item'><span class='nav-text'>探索展室</span></a><a href='/stylequiz/?_encoding=UTF8&ref_=nav_wishlist_sq_ya_yl' class='nav-link nav-item'><span class='nav-text'>参加家庭风格测验</span></a><a class='nav-link nav-item'><span class='nav-text'></span></a></div><div id='nav-al-your-account' class='nav-al-column nav-template nav-flyout-content nav-tpl-itemList'><div class='nav-title'>我的账户</div><a href='/gp/css/homepage.html?ie=UTF8&ref_=nav_youraccount_ya' class='nav-link nav-item'><span class='nav-text'>我的账户</span></a><a href='/gp/css/order-history?ie=UTF8&ref_=nav_youraccount_orders' class='nav-link nav-item' id='nav_prefetch_yourorders'><span class='nav-text'>我的订单</span></a><a href='/gp/registry/wishlist?ie=UTF8&ref_=nav_youraccount_wl&requiresSignIn=1' class='nav-link nav-item'><span class='nav-text'>我的心愿单</span></a><a href='/gp/yourstore?ie=UTF8&ref_=nav_youraccount_recs' class='nav-link nav-item'><span class='nav-text'>为我推荐</span></a><a href='/gp/subscribe-and-save/manager/viewsubscriptions?ie=UTF8&ref_=nav_youraccount_sns' class='nav-link nav-item'><span class='nav-text'>我的”订购与节省“商品</span></a><a href='/yourmembershipsandsubscriptions?_encoding=UTF8&ref_=nav_youraccount_digital_subscriptions' class='nav-link nav-item'><span class='nav-text'>成员资格和订阅</span></a><a href='/localservices/ya/servicerequests?_encoding=UTF8&ref_=nav_youraccount_desktop_vas_requestlist' class='nav-link nav-item'><span class='nav-text'>我的服务请求</span></a><a href='/gp/your-garage/?ie=UTF8&ref_=nav_youraccount_au_pf_as_GNO' class='nav-link nav-item'><span class='nav-text'>我的车库</span></a><a href='/fanshop?_encoding=UTF8&ref_=nav_youraccount_yfs' class='nav-link nav-item'><span class='nav-text'>您的运动粉丝商店</span></a><a href='/yourpets?_encoding=UTF8&ref_=nav_youraccount_pet_profiles' class='nav-link nav-item'><span class='nav-text'>我的宠物</span></a><a href='/mn/dcw/myx.html?_encoding=UTF8&ref_=nav_youraccount_myk' class='nav-link nav-item'><span class='nav-text'>管理我的内容和设备</span></a><a href='/gp/dmusic/mp3/player?ie=UTF8&ref_=nav_youraccount_cldplyr' class='nav-link nav-item'><span class='nav-text'>我的音乐库</span></a><a href='/clouddrive?_encoding=UTF8&ref_=nav_youraccount_clddrv' class='nav-link nav-item'><span class='nav-text'>我的Amazon Drive</span></a><a href='/Prime-Instant-Video/s/browse?_encoding=UTF8&node=2676882011&ref_=nav_youraccount_piv' class='nav-link nav-item'><span class='nav-text'>Prime 即时录像</span></a><a href='/gp/kindle/ku/ku_central?ie=UTF8&ref_=nav_youraccount_ku' class='nav-link nav-item'><span class='nav-text'>我的 Kindle Unlimited</span></a><a href='/gp/video/watchlist?ie=UTF8&ref_=nav_youraccount_ywl' class='nav-link nav-item'><span class='nav-text'>我的播放列表</span></a><a href='/gp/video/library?ie=UTF8&ref_=nav_youraccount_yvl' class='nav-link nav-item'><span class='nav-text'>我的录像库</span></a><a href='/gp/mas/your-account/myapps?ie=UTF8&ref_=nav_youraccount_aad' class='nav-link nav-item'><span class='nav-text'>我的应用程序和设备</span></a><a class='nav-link nav-item'><span class='nav-text'></span></a></div></div>"},"templates":{"asin-promo":"<a href='<#=destination #>' class='nav_asin_promo'> <img src='<#=image #>' class='nav_asin_promo_img'/> <span class='nav_asin_promo_headline'><#=headline #></span> <span class='nav_asin_promo_info'> <span class='nav_asin_promo_title'><#=productTitle #></span> <span class='nav_asin_promo_title2'><#=productTitle2 #></span> <span class='nav_asin_promo_price'><#=price #></span> </span> <span class='nav_asin_promo_button nav-sprite'><#=button #></span></a>","discoveryPanelList":"<# var renderItems = function(items) { #> <span class='nav-dp-title nav-item'> Deliveries at a glance <div class='nav-divider-container'><div class='nav-divider'></div></div></span> <# jQuery.each(items, function (i, item) { #> <span class='nav-item'> <a href='<#=item.order_link#>' class='nav-dp-link'> <span class='nav-dp-left-column'> <img src='<#=item.image#>' class='nav-dp-image'/> </span> <span class='nav-dp-right-column'> <span class='nav-dp-text <#=item.status#>'> <#=item.status_text#> <br/> </span> <# if(item.secondary_status_text) { #> <span class='nav-dp-text-secondary <#=item.status#>'> <#=item.secondary_status_text#> </span> <# } #> </span> </a> <div class='nav-divider-container'><div class='nav-divider'></div></div> </span> <# }); #> <a href='/your-orders/ref=nav_dp_ayo' class='nav-dp-link-emphasis'> View all orders </a><# }; #><# renderItems(items); #>","itemList":"<# var hasColumns = (function () { var checkColumns = function (_items) { if (!_items) { return false; } for (var i=0; i<_items.length; i++) { if (_items[i].columnBreak || (_items[i].items && checkColumns(_items[i].items))) { return true; } } return false; }; return checkColumns(items);}()); #><# if(hasColumns) { #> <# if(items[0].image && items[0].image.src) { #> <div class='nav-column nav-column-first nav-column-image'> <# } else if (items[0].greeting) { #> <div class='nav-column nav-column-first nav-column-greeting'> <# } else { #> <div class='nav-column nav-column-first'> <# } #><# } #><# var renderItems = function(items) { #> <# jQuery.each(items, function (i, item) { #> <# if(hasColumns && item.columnBreak) { #> <# if(item.image && item.image.src) { #> </div><div class='nav-column nav-column-notfirst nav-column-break nav-column-image'> <# } else if (item.greeting) { #> </div><div class='nav-column nav-column-notfirst nav-column-break nav-column-greeting'> <# } else { #> </div><div class='nav-column nav-column-notfirst nav-column-break'> <# } #> <# } #> <# if(item.dividerBefore) { #> <div class='nav-divider'></div> <# } #> <# if(item.text || item.content) { #> <# if(item.url) { #> <a href='<#=item.url #>' class='nav-link <# } else {#> <span class=' <# } #> <# if(item.panelKey) { #> nav-hasPanel <# } #> <# if(item.items) { #> nav-title <# } #> <# if(item.decorate == 'carat') { #> nav-carat <# } #> <# if(item.decorate == 'nav-action-button') { #> nav-action-button <# } #> nav-item' <# if(item.extra) { #> <#=item.extra #> <# } #> <# if(item.id) { #> id='<#=item.id #>' <# } #> <# if(item.dataNavRole) { #> data-nav-role='<#=item.dataNavRole #>' <# } #> <# if(item.dataNavRef) { #> data-nav-ref='<#=item.dataNavRef #>' <# } #> <# if(item.panelKey) { #> data-nav-panelkey='<#=item.panelKey #>' role='navigation' aria-label='<#=item.text#>' <# } #> <# if(item.subtextKey) { #> data-nav-subtextkey='<#=item.subtextKey #>' <# } #> <# if(item.image && item.image.height > 16) { #> style='line-height:<#=item.image.height #>px;' <# } #> > <# if(item.decorate == 'carat') { #> <i class='nav-icon'></i> <# } #> <# if(item.image && item.image.src) { #> <img class='nav-image' src='<#=item.image.src #>' style='height:<#=item.image.height #>px; width:<#=item.image.width #>px;' /> <# } #> <# if(item.text) { #> <span class='nav-text<# if(item.classname) { #> <#=item.classname #><# } #>'><#=item.text#><# if(item.badgeText) { #> <span class='nav-badge'><#=item.badgeText#></span> <# } #></span> <# } else if (item.content) { #> <span class='nav-content'><# jQuery.each(item.content, function (j, cItem) { #><# if(cItem.url && cItem.text) { #><a href='<#=cItem.url #>' class='nav-a'><#=cItem.text #></a><# } else if (cItem.text) { #><#=cItem.text#><# } #><# }); #></span> <# } #> <# if(item.subtext) { #> <span class='nav-subtext'><#=item.subtext #></span> <# } #> <# if(item.url) { #> </a> <# } else {#> </span> <# } #> <# } #> <# if(item.image && item.image.src) { #> <# if(item.url) { #> <a href='<#=item.url #>'> <# } #> <img class='nav-image' <# if(item.id) { #> id='<#=item.id #>' <# } #> src='<#=item.image.src #>' <# if (item.alt) { #> alt='<#= item.alt #>'<# } #>/> <# if(item.url) { #> </a> <# } #> <# } #> <# if(item.items) { #> <div class='nav-panel'> <# renderItems(item.items); #> </div> <# } #> <# }); #><# }; #><# renderItems(items); #><# if(hasColumns) { #> </div><# } #>","notificationsList":"<div class='nav-item nav-title'> 通知</div><# jQuery.each(items || [], function (i, item) { #> <div class='nav-item<# if (item.type) { #> nav-noti-list-<#= item.type #><# } #><# if (item.image && item.image.src) { #> nav-noti-list-with-image<# } #>'> <# if (item.dismissId) { #> <div class='nav-noti-list-x' data-noti-id='<#= item.dismissId #>'>×</div> <# } #> <# if (item.image && item.image.src) { #> <div class='nav-noti-list-image'> <img class='nav-noti-list-image-tag' src='<#= item.image.src #>' <# if (item.image.alt) { #> alt='<#= item.image.alt #>'<# } #> <# if (item.image.title) { #> title='<#= item.image.title #>'<# } #>/> </div> <# } #> <# if (item.heading) { #> <div class='nav-noti-list-heading'><#= item.heading #></div> <# } #> <# jQuery.each(item.content || [], function (j, itemContent) { #> <# if (itemContent.url) { #> <a href='<#= itemContent.url #>' class='nav-noti-list-content'> <# } else { #> <div class='nav-noti-list-content'> <# } #> <# if (itemContent.text) { #> <span class='nav-noti-list-text'><#= itemContent.text #></span> <# } #> <# if (itemContent.subtext) { #> <span class='nav-noti-list-subtext'><#= itemContent.subtext #></span> <# } #> <# if (itemContent.url) { #> </a> <# } else { #> </div> <# } #> <# }); #> </div><# }); #>","discoveryPanelSummary":" <span class='nav-dp-title nav-item'> Deliveries at a glance <div class='nav-divider-container'><div class='nav-divider'></div></div></span> <# jQuery.each(items || [], function (i, item) { #> <span class='nav-item'> <span class='nav-dp-left-column'> <img src='<#=item.image.url#>' class='nav-dp-image' height='<#=item.image.height#>'/> </span> <span class='nav-dp-right-column'> <#=item.status_text#> <div class='nav-dp-secondary-row'> <a href='/your-orders/ref=nav_dp_ryo' class='nav-dp-link-emphasis'> Sign in to view orders </a> </div> </span> </span> <# }); #>","htmlList":" <# jQuery.each(items, function (i, item) { #> <div class='nav-item'> <#=item #> </div> <# }); #>","subnav":"<# if (obj && obj.type === 'vertical') { #> <# jQuery.each(obj.rows, function (i, row) { #> <# if (row.flyoutElement === 'button') { #> <div class='nav_sv_fo_v_button' <# if (row.elementStyle) { #> style='<#= row.elementStyle #>' <# } #> > <a href='<#=row.url #>' class='nav-action-button nav-sprite'> <#=row.text #> </a> </div> <# } else if (row.flyoutElement === 'list' && row.list) { #> <# jQuery.each(row.list, function (j, list) { #> <div class='nav_sv_fo_v_column <#=(j === 0) ? 'nav_sv_fo_v_first' : '' #>'> <ul class='<#=list.elementClass #>'> <# jQuery.each(list.linkList, function (k, link) { #> <# if (k === 0) { link.elementClass += ' nav_sv_fo_v_first'; } #> <li class='<#=link.elementClass #>'> <# if (link.url) { #> <a href='<#=link.url #>' class='nav_a'><#=link.text #></a> <# } else { #> <span class='nav_sv_fo_v_span'><#=link.text #></span> <# } #> </li> <# }); #> </ul> </div> <# }); #> <# } else if (row.flyoutElement === 'link') { #> <# if (row.topSpacer) { #> <div class='nav_sv_fo_v_clear'></div> <# } #> <div class='<#=row.elementClass #>'> <a href='<#=row.url #>' class='nav_sv_fo_v_lmargin nav_a'> <#=row.text #> </a> </div> <# } #> <# }); #><# } else if (obj) { #> <div class='nav_sv_fo_scheduled'> <#= obj #> </div><# } #>","wishlist":"<# jQuery.each(wishlist, function (i, item) { #> <li class='nav_pop_li'> <a href='<#=item.url #>' class='nav_a'> <#=item.name #> </a> <div class='nav_tag'> <!-- TODO this logic should now be in dynamic-wish-list.mi --> <# if(typeof item.count !='undefined') { #> <#= (item.count == 1 ? '{count} 商品' : '{count} 商品') .replace('{count}', item.count) #> <# } #> </div> </li><# }); #>","cart":"<# jQuery.each(items, function (i, item) { #> <div class='nav-cart-item'> <a href='<#=item.url #>' class='nav-cart-item-link'> <img src='<#=item.img #>' class='nav-cart-item-image' /> <span class='nav-cart-item-title'><#=item.name #></span> <# if (item.weight) { #> <span class='nav-cart-item-weight' style='display:none;'> <#= ''.replace('{value}', item.weight.value).replace('{unit}', item.weight.unit) #> </span> <# } #> <# if (item.ourPrice) { #> <span class='nav-cart-item-buyingPrice'><#=item.ourPrice #></span> <# } #> <# if (item.scarcityMessage) { #> <span class='<#=item.scarcityClass #>'><#=item.scarcityMessage #></span> <# } #> <span class='nav-cart-item-quantity'> <#= '数量: {count}'.replace('{count}', item.qty) #> </span> </a> </div> <# if (i%2==1) { #> <div class='nav-cart-item-break'></div> <# } #><# }); #><div class='nav-cart-item-break'></div>"}}); });
这是结果:
Prime视频 /s/browse?_encoding=UTF8&node=2858778011&ref_=nav_shopall-export_nav_mw_sbd_intl_video
音乐、CD和乙烯基塑料唱片 /s/browse?_encoding=UTF8&node=5174&ref_=nav_shopall-export_nav_mw_sbd_intl_cd
数字音乐 /s/browse?_encoding=UTF8&node=163856011&ref_=nav_shopall-export_nav_mw_sbd_intl_music
Kindle 商店 /s/browse?_encoding=UTF8&node=133140011&ref_=nav_shopall-export_nav_mw_sbd_intl_kindle
工艺品 /s/browse?_encoding=UTF8&node=4954955011&ref_=nav_shopall-export_nav_mw_sbd_intl_arts
汽车 /s/browse?_encoding=UTF8&node=2562090011&ref_=nav_shopall-export_nav_mw_sbd_intl_automotive
婴儿用品 /s/browse?_encoding=UTF8&node=16225005011&ref_=nav_shopall-export_nav_mw_sbd_intl_baby
美容和个人护理 /s/browse?_encoding=UTF8&node=16225006011&ref_=nav_shopall-export_nav_mw_sbd_intl_beauty
图书 /s/browse?_encoding=UTF8&node=283155&ref_=nav_shopall-export_nav_mw_sbd_intl_books
计算机 /s/browse?_encoding=UTF8&node=16225007011&ref_=nav_shopall-export_nav_mw_sbd_intl_computers
电子产品 /s/browse?_encoding=UTF8&node=16225009011&ref_=nav_shopall-export_nav_mw_sbd_intl_electronics
女士时尚 /s/browse?_encoding=UTF8&node=16225018011&ref_=nav_shopall-export_nav_mw_sbd_intl_womenfasion
男士时尚 /s/browse?_encoding=UTF8&node=16225019011&ref_=nav_shopall-export_nav_mw_sbd_intl_menfasion
女童时尚 /s/browse?_encoding=UTF8&node=16225020011&ref_=nav_shopall-export_nav_mw_sbd_intl_girlfasion
男童时尚 /s/browse?_encoding=UTF8&node=16225021011&ref_=nav_shopall-export_nav_mw_sbd_intl_boyfasion
健康和家居用品 /s/browse?_encoding=UTF8&node=16225010011&ref_=nav_shopall-export_nav_mw_sbd_intl_health
家居厨房用品 /s/browse?_encoding=UTF8&node=16225011011&ref_=nav_shopall-export_nav_mw_sbd_intl_kitchen
工业科技 /s/browse?_encoding=UTF8&node=16225012011&ref_=nav_shopall-export_nav_mw_sbd_intl_industrial
行李箱包 /s/browse?_encoding=UTF8&node=16225017011&ref_=nav_shopall-export_nav_mw_sbd_intl_luggage
电影和电视 /s/browse?_encoding=UTF8&node=2625373011&ref_=nav_shopall-export_nav_mw_sbd_intl_movies
宠物用品 /s/browse?_encoding=UTF8&node=16225013011&ref_=nav_shopall-export_nav_mw_sbd_intl_pet
软件 /s/browse?_encoding=UTF8&node=16225008011&ref_=nav_shopall-export_nav_mw_sbd_intl_software
运动与户外用品 /s/browse?_encoding=UTF8&node=16225014011&ref_=nav_shopall-export_nav_mw_sbd_intl_sports
工具与家居装饰 /s/browse?_encoding=UTF8&node=256643011&ref_=nav_shopall-export_nav_mw_sbd_intl_tools
玩具与游戏 /s/browse?_encoding=UTF8&node=16225015011&ref_=nav_shopall-export_nav_mw_sbd_intl_toys
视频游戏 /s/browse?_encoding=UTF8&node=16225016011&ref_=nav_shopall-export_nav_mw_sbd_intl_video_games
销售和优惠 /s/browse?_encoding=UTF8&node=15529609011&ref_=nav_shopall-export_nav_mw_sbd_intl_sales
源码:
#! /usr/bin/env python
# # -*- coding: utf-8 -*-
import requests
import re
import json
headers = {
'User-Agent': '',
'Accept-Language': 'zh-CN'
}
req = requests.get("https://www.amazon.com/", headers=headers, verify=False)
content = req.text
keyword = 'wishlistContent'
# 解析列表
def handle(s, keyword):
if s.find(keyword) < 0:
return False
b = s.find('data({') + 5
e = s.rfind('}); });') + 1
j = json.loads(s[b:e])
items = j['shopAllContent']['template']['data']['items']
for item in items:
print(item['text'], item['url'])
return True
pattern = re.compile(r'<script[^>]*>((?!</script>).)*</script>', re.DOTALL)
pos = 0
while True:
match = pattern.search(content, pos)
if not match:
break
s = match.start()
e = match.end()
if handle(content[s:e], keyword):
break
pos = e
爬虫——简单处理js中嵌入的json数据的更多相关文章
- js中如何操作json数据
一.要想熟练的操作json数据,就先要了解json数据的结构,json有两种结构:对象和数组. 1.对象 一个对象以“{”开始,“}”结束.每个“名称”后跟一个“:”:“‘名称/值’ 对”之间使用“, ...
- js中读取解析json数据
在数据传输流程中,json是以文本,即字符串的形式传递的,而JS操作的是JSON对象,所以,JSON对象和JSON字符串之间的相互转换是关键. JSON字符串: 'var str1 = ' ...
- JS中eval处理JSON数据 为什么要加括号
由于Ajax的兴起,JSON这种轻量级的数据格式作为客户端与服务器之间的传输格式逐渐地流行起来,进而出现的问题是如何将服务器端构建好的JSON数据转化为可用的JavaScript对象.利用eval函数 ...
- 在 Node.js 中处理大 JSON 文件
在 Node.js 中处理大 JSON 文件 场景描述 问题一: 假设现在有一个场景,有一个大的 JSON 文件,需要读取每一条数据经过处理之后输出到一个文件或生成报表数据,怎么能够流式的每次读取一条 ...
- 问题集录--JS如何处理和解析Json数据
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.它基于ECMAScript的一个子集. JSON采用完全独立于语言的文本格式,但是也使用了类似于C语言家族 ...
- Swift语言中如何使用JSON数据教程
这是一篇翻译文章,原文出处:http://www.raywenderlich.com/82706/working-with-json-in-swift-tutorial Swift语言中如何使用JSO ...
- js进阶ajax读取json数据(ajax读取json和读取普通文本,和获取服务器返回数据(链接)都是一样的,在url处放上json文件的地址即可)
js进阶ajax读取json数据(ajax读取json和读取普通文本,和获取服务器返回数据(链接)都是一样的,在url处放上json文件的地址即可) 一.总结 ajax读取json和读取普通文本,和获 ...
- WPF:使用Json.NET在TreeView中树形显示JSON数据
原文 WPF:使用Json.NET在TreeView中树形显示JSON数据 据 读者可以参考这个开源的可以树形显示XML和JSON的工具: Mgen Object 603:XML/JSON树形显示小工 ...
- 简单谈谈js中的MVC
MVC是什么? MVC是一种架构模式,它将应用抽象为3个部分:模型(数据).视图.控制器(分发器). 本文将用一个经典的例子todoList来展开(代码在最后). 一个事件发生的过程(通信单向流动): ...
随机推荐
- Java入门第二季——Java中的this关键字
如果想访问一个private属性,通常会使用setter和getter方法来操作属性,属性名经常会与参数名相同,我们为了区分属性和参数的概念,我们会在属性前面加上this关键字,此时代表我将一个参数的 ...
- 为什么说Redis是单线程的以及Redis为什么这么块
一.前言 近乎所有与Java相关的面试都会问到缓存的问题,基础一点的会问到什么是“二八定律”.什么是“热数据和冷数据”,复杂一点的会问到缓存雪崩.缓存穿透.缓存预热.缓存更新.缓存降级等问题,这些看似 ...
- Vue学习日记(三)——Vue路由管理vue-router
前言 为了给读者更好的体验,去理解vue-router和下一篇介绍vuex,决定还是来一个实战教程来带大家更加的去深入理解vue-router,在这之前,读者先自行了解和去官网下载npm和node,附 ...
- mybatis标签selectkey无法返回主键值
- java实现上传文件夹
我们平时经常做的是上传文件,上传文件夹与上传文件类似,但也有一些不同之处,这次做了上传文件夹就记录下以备后用. 首先我们需要了解的是上传文件三要素: 1.表单提交方式:post (get方式提交有大小 ...
- Codevs 1574 广义斐波那契数列(矩阵乘法)
1574 广义斐波那契数列 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 钻石 Diamond 题目描述 Description 广义的斐波那契数列是指形如an=p*an-1+q* ...
- Maximum GCD(UVA 11827)
Problem:Given the N integers, you have to find the maximum GCD (greatest common divisor) of every po ...
- 微信打开手机内置浏览器跳转手机默认浏览器打开html网页
微信上进行的网页宣传.游戏传播.APP下载各类活动很多,但是各位朋友肯定经常会遇到一些特殊需求,网页需要在手机默认浏览器打开而不是微信内置浏览器.这个问题怎么解决呢? 斗在微信营销的浪潮中 解决方案: ...
- DbFunctions 作为 LINQ to Entities 查询的一部分使用时,此方法调用规范 CreateDateTime EDM 函数以创建新的 DateTime 对象。
DbFunctions.CreateDateTime CreateDateTime(year, month,day,hour,minute,second)
- mysql 查看当前正在执行的语句
查看当前正在执行的语句 show processlist:show processlist; 结束正在执行的语句进程 kill 进程id