爬虫——简单处理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&amp;associationHandle=usflex&amp;currentPageURL=https%3A%2F%2Fwww.amazon.com%2Fgp%2Fyourstore%2Fhome%3Fie%3DUTF8%26ref_%3Dnav_signin&amp;pageType=&amp;switchAccount=&amp;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&amp;openid.assoc_handle=usflex&amp;openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&amp;openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&amp;openid.mode=checkid_setup&amp;openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&amp;openid.ns.pape=http%3A%2F%2Fspecs.openid.net%2Fextensions%2Fpape%2F1.0&amp;openid.pape.max_auth_age=0&amp;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&amp;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&amp;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&amp;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&currentPageURL=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&amp;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&amp;associationHandle=usflex&amp;currentPageURL=https%3A%2F%2Fwww.amazon.com%2Fgp%2Fyourstore%2Fhome%3Fie%3DUTF8%26ref_%3Dnav_custrec_signin&amp;pageType=&amp;switchAccount=&amp;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&amp;openid.assoc_handle=usflex&amp;openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&amp;openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&amp;openid.mode=checkid_setup&amp;openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&amp;openid.ns.pape=http%3A%2F%2Fspecs.openid.net%2Fextensions%2Fpape%2F1.0&amp;openid.pape.max_auth_age=0&amp;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&amp;associationHandle=usflex&amp;currentPageURL=https%3A%2F%2Fwww.amazon.com%2Fgp%2Fyourstore%2Fhome%3Fie%3DUTF8%26ref_%3Dnav_signin&amp;pageType=&amp;switchAccount=&amp;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&amp;openid.assoc_handle=usflex&amp;openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&amp;openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&amp;openid.mode=checkid_setup&amp;openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&amp;openid.ns.pape=http%3A%2F%2Fspecs.openid.net%2Fextensions%2Fpape%2F1.0&amp;openid.pape.max_auth_age=0&amp;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&amp;ref_=nav_wishlist_create&amp;triggerElementID=createList' class='nav-link nav-item'><span class='nav-text'>创建一个心愿单</span></a><a href='/gp/registry/search?ie=UTF8&amp;ref_=nav_wishlist_find' class='nav-link nav-item'><span class='nav-text'>查找其他人的心愿单</span></a><a href='/gcx/Gifts-for-Adults/gfhz/?_encoding=UTF8&amp;ref_=nav_wishlist_gf' class='nav-link nav-item'><span class='nav-text'>查找礼品</span></a><a href='/gp/BIT?ie=UTF8&amp;bitCampaignCode=a0032&amp;ref_=nav_wishlist_bit_v2_a0032' class='nav-link nav-item'><span class='nav-text'>保存Web中的商品</span></a><a href='/wedding/home?_encoding=UTF8&amp;ref_=nav_wishlist_wr' class='nav-link nav-item'><span class='nav-text'>婚庆礼品登记清单</span></a><a href='/baby-reg/homepage?_encoding=UTF8&amp;ref_=nav_wishlist_br' class='nav-link nav-item'><span class='nav-text'>婴儿礼品登记清单</span></a><a href='/gp/gift-central/organizer?ie=UTF8&amp;ref_=nav_wishlist_fafgift' class='nav-link nav-item'><span class='nav-text'>亲友馈赠</span></a><a href='/gp/clpf?ie=UTF8&amp;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&amp;ref_=nav_wishlist_pntry_gno' class='nav-link nav-item'><span class='nav-text'>Pantry 清单</span></a><a href='/stream/saves/items?_encoding=UTF8&amp;ref_=nav_wishlist_strm_in_youtique_lists' class='nav-link nav-item'><span class='nav-text'>我的中心</span></a><a href='/ideas?_encoding=UTF8&amp;ref_=nav_wishlist_idea_nav' class='nav-link nav-item'><span class='nav-text'>探索创意列表</span></a><a href='/showroom?_encoding=UTF8&amp;ref_=nav_wishlist_srm_your_desk_wl' class='nav-link nav-item'><span class='nav-text'>探索展室</span></a><a href='/stylequiz/?_encoding=UTF8&amp;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&amp;ref_=nav_youraccount_ya' class='nav-link nav-item'><span class='nav-text'>我的账户</span></a><a href='/gp/css/order-history?ie=UTF8&amp;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&amp;ref_=nav_youraccount_wl&amp;requiresSignIn=1' class='nav-link nav-item'><span class='nav-text'>我的心愿单</span></a><a href='/gp/yourstore?ie=UTF8&amp;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&amp;ref_=nav_youraccount_sns' class='nav-link nav-item'><span class='nav-text'>我的”订购与节省“商品</span></a><a href='/yourmembershipsandsubscriptions?_encoding=UTF8&amp;ref_=nav_youraccount_digital_subscriptions' class='nav-link nav-item'><span class='nav-text'>成员资格和订阅</span></a><a href='/localservices/ya/servicerequests?_encoding=UTF8&amp;ref_=nav_youraccount_desktop_vas_requestlist' class='nav-link nav-item'><span class='nav-text'>我的服务请求</span></a><a href='/gp/your-garage/?ie=UTF8&amp;ref_=nav_youraccount_au_pf_as_GNO' class='nav-link nav-item'><span class='nav-text'>我的车库</span></a><a href='/fanshop?_encoding=UTF8&amp;ref_=nav_youraccount_yfs' class='nav-link nav-item'><span class='nav-text'>您的运动粉丝商店</span></a><a href='/yourpets?_encoding=UTF8&amp;ref_=nav_youraccount_pet_profiles' class='nav-link nav-item'><span class='nav-text'>我的宠物</span></a><a href='/mn/dcw/myx.html?_encoding=UTF8&amp;ref_=nav_youraccount_myk' class='nav-link nav-item'><span class='nav-text'>管理我的内容和设备</span></a><a href='/gp/dmusic/mp3/player?ie=UTF8&amp;ref_=nav_youraccount_cldplyr' class='nav-link nav-item'><span class='nav-text'>我的音乐库</span></a><a href='/clouddrive?_encoding=UTF8&amp;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&amp;node=2676882011&amp;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&amp;ref_=nav_youraccount_ku' class='nav-link nav-item'><span class='nav-text'>我的 Kindle Unlimited</span></a><a href='/gp/video/watchlist?ie=UTF8&amp;ref_=nav_youraccount_ywl' class='nav-link nav-item'><span class='nav-text'>我的播放列表</span></a><a href='/gp/video/library?ie=UTF8&amp;ref_=nav_youraccount_yvl' class='nav-link nav-item'><span class='nav-text'>我的录像库</span></a><a href='/gp/mas/your-account/myapps?ie=UTF8&amp;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 #>'>&times;</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

posted on 2019-05-07 18:29  lichao890427  阅读(959)  评论(0编辑  收藏  举报

导航