Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 机械化刮削_Python_Html_Web Scraping_Mechanize - Fatal编程技术网

Python 机械化刮削

Python 机械化刮削,python,html,web-scraping,mechanize,Python,Html,Web Scraping,Mechanize,我遇到了一个问题,mechanize不能产生与浏览器相同的响应。我正试图从这个允许使用预先填充的url向购物篮中添加商品的网页中获取价格 我所拥有的是: import mechanize import urllib import cookielib import BeautifulSoup import html2text url='http://store.nike.com/us/services/jcartService?callback=nike_Cart_hanleJCartResp

我遇到了一个问题,mechanize不能产生与浏览器相同的响应。我正试图从这个允许使用预先填充的url向购物篮中添加商品的网页中获取价格

我所拥有的是:

import mechanize
import urllib
import cookielib
import BeautifulSoup
import html2text

url='http://store.nike.com/us/services/jcartService?callback=nike_Cart_hanleJCartResponse&action=addItem&lang_locale=en_US&country=US&catalogId=1&productId=463712&price=00.0&siteId=null&line1=Nike+Air+Max+1+Ultra+Moire&line2=Men%27s+Shoe&passcode=null&sizeType=null&skuAndSize=10661133%3A10&qty=1&rt=json&view=3&skuId=10661133&displaySize=14&_=142655682313'

br = mechanize.Browser()
cj = cookielib.LWPCookieJar()
br.set_cookiejar(cj)
br.set_handle_equiv(True)
br.set_handle_gzip(False)
br.set_handle_redirect(True)
br.set_handle_referer(False)
br.set_handle_robots(True)
br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1)
br.addheaders = [('User-agent', 'Chrome')]

br.open(url)
pageText=br.open(url).read()
print pageText
然后,我计划进行一些基本的字符串解析以获得价格。问题是,当我刮掉页面时,我得到了以下信息:

打印页面文本

nike_Cart_hanleJCartResponse({
    "exceptions": [{
        "property": "catalogRefId",
        "errorcode": "noItemsToAddInStock",
        "message": "Sorry, Ѿ, this item isn't available anymore because other shoppers added it to their carts first. But if they don't check out in the next few minutes, you've still got a chance. Please try again shortly. [Code: 420N-00000000]"
    }],
    "status": "failure",
    "order": {
        "id": "dummy",
        "objType": "order",
        "itemQuantity": 0
    }
});
当它应该像在浏览器中一样返回这样的内容时:

nike_Cart_hanleJCartResponse({
    "status" :"success","order" :{
        "id" :"O1014750586",
        "objType" :"order",
        "itemQuantity" :1,
        "priceInfo" :{
            "currencyFormat" :"$0.00",
            "currency" :"USD",
            "amount" :"75.0",
            ....
}]}]}});
我查看了lxml,但对如何操作感到相当困惑。无法正确地刮除这一页吗


任何帮助都将不胜感激。提前谢谢

首先导航到主存储页面,以便您可以获得正确的cookie。然后导航到所需的URL:

import mechanize

store_url = 'http://store.nike.com'
cart_url = 'http://store.nike.com/us/services/jcartService?callback=nike_Cart_hanleJCartResponse&action=addItem&lang_locale=en_US&country=US&catalogId=1&productId=463712&price=00.0&siteId=null&line1=Nike+Air+Max+1+Ultra+Moire&line2=Men%27s+Shoe&passcode=null&sizeType=null&skuAndSize=10661133%3A10&qty=1&rt=json&view=3&skuId=10661133&displaySize=14&_=142655682313'

br = mechanize.Browser()
response = br.open(store_url)
response = br.open(cart_url)
data = response.read()
print data
输出

nike_Cart_hanleJCartResponse({"status" :"success","order" :{"id" :"O1014976420","objType" :"order","itemQuantity" :1,"priceInfo" :{"currencyFormat" :"$0.00","currency" :"USD","amount" :"75.0","isDiscounted" :false,"formattedAmount" :"$75.00","subTotal" :75.0,"formattedSubTotal" :"$75.00","discountAmount" :0.0,"formattedDiscountAmount" :"$0.00","tax" :0.0,"rawSubtotal" :75.0,"formattedRawSubtotal" :"$75.00","formattedTax" :"$0.00","shipping" :0.0,"formattedShipping" :"$0.00","rawShipping" :0.0,"formattedRawShipping" :"$0.00","giftWrap" :0.0,"formattedGiftWrap" :"$0.00","total" :75.0,"formattedTotal" :"$75.00"},"originOfOrder" :"0","transient" :false,"shippingGroups" :[{"id" :"SG1022772456","objType" :"shippingGroup","priceInfo" :{"currencyFormat" :"$0.00","currency" :"USD","amount" :"0.0","isDiscounted" :false,"formattedAmount" :"$0.00","rawShipping" :0.0,"formattedRawShipping" :"$0.00"},"shippingMethod" :"Ground Service","description" :"me","shippingMethodDisplay" :"Standard","commerceItems" :[{"id" :"CI1022253932","objType" :"commerceItem","priceInfo" :{"currencyFormat" :"$0.00","currency" :"USD","amount" :"75.0","isDiscounted" :false,"formattedAmount" :"$75.00","listPrice" :75.0,"formattedListPrice" :"$75.00","salePrice" :0.0,"formattedSalePrice" :"$0.00","onSale" :false,"rawTotalPrice" :75.0,"formattedRawTotalPrice" :"$75.00","giftWrapPrice" :0.0,"formattedGiftWrapPrice" :"$0.00"},"validForCountry" :true,"commerceItemClassType" :"default","quantity" :1,"catalogRefId" :"10661133","catalogId" :"1","product" :{"id" :"463712","view" :"short","displayName" :"Nike Roshe Run Men's Shoe","description" :"Nike Roshe Run Men's Shoe","colorNumber" :"010","activeIndicator" :"ACTIVE","type" :"nikeProduct","styleNumber" :"511881","salePrice" :"75.0","listPrice" :"75.0","employeePrice" :"45.0","onSale" :false,"currency" :"USD","currencyFormat" :"$0.00","formattedListPrice" :"$75.00","formattedSalePrice" :"$75.00","colorDescription" :"Black/Sail/Anthracite","prdGroupId" :"943980","nikeType" :"FOOTWEAR","dynamicAttributes" :{"includepromo" :"GIFTS2014","nikeidmatchstyle" :"704691","width" :"Regular","nikeidmatch" :"true","publishdate" :"05/11/2010","nikeidmatchproductid" :"1094199","divisioncode" :"20","productdisplayorder" :"7","simplecolor" :"BLACK","quantitylimit" :"2","giftwrap" :"true","modelType" :"FOOTWEAR","swatchcolorhex" :"000000","gender" :"Men","classid" :"120001"}},"shipTo" :null,"giftMessage" :null,"giftMessageType" :0,"itemSource" :"SC","sizeType" :"","displaySize" :"14","sizeDescription" :"14","eanNumber" :"00675911199978","colorNumber" :"010","colorDescription" :"Black/Sail/Anthracite"}]}]}}); nike_Cart_hanleJCartResponse({“状态”:“成功”,“订单”:{“id”:“O1014976420”,“对象类型”:“订单”,“项目数量”:1,“价格信息”:{“currencyFormat”:“$0.00”,“货币”:“USD”,“金额”:“75.0”,“isDiscounted”:false,“格式化金额”:“$75.00”,“小计”:75.0,“格式化小计”:“$75.00”,“折扣金额”:0.0”,“格式化折扣金额”:,“税”:0.0,“工资小计”:75.0,“工资小计”:“$75.00”,“工资税”:“$0.00”,“运费”:0.0,“工资小计”:“$0.00”,“工资小计”:0.0,“工资小计”:“$0.00”,“工资小计”:0.0,“工资小计”:“$0.00”,“总计”:75.0,“工资总额”:“$75.00”},“原始订单”:“0”,“临时”:假,“发货组”:“{“id”:”SG1022772456,“objType:“shippingGroup”,“priceInfo:{”currencyFormat:“$0.00”,“currency:“USD”,“amount:”0.0”,“isDiscounted:”false,“formattedAmount:”$0.00”,“rawShipping:”0.0,“formattedRawShipping:“$0.00”},“shippingMethod:“地面服务”,“描述:”“me”,“shippingMethodDisplay:”标准”,“商业项目:[{”id:“CI1022253932”objType:“commerceItem”,“priceInfo:{”currencyFormat:“$0.00”,“currency:“USD”,“amount:“75.0”,“isDiscounted:”false,“formattedAmount:”$75.00”,“listPrice:”75.0,“formattedListPrice:“$75.00”,“salePrice:”0.0,“formattedSalePrice:”$0.00”,“onSale:”false:“rawTotalPrice:”75.0,“formattedRawTotalPrice:“$75.00”,“GiftRawTrapPrice:”0.0,”格式化的GiftRapPrice“$0.00”},“validForCountry”:true,“commerceItemClassType”:“default”,“quantity”:1,“catalogRefId”:“10661133”,“catalogId”:“1”,“product”:{“id”:“463712”,“view”:“short”,“displayName”:“Nike Roshe Run男子运动鞋”,“description”:“Nike Roshe Run男子运动鞋”,“colorNumber”:“010”,“activeIndicator”:“ACTIVE”,“type”:“nikeProduct”“,”styleNumber“:”511881“,”salePrice“:”75.0“,”listPrice“:”75.0“,”employeePrice“:”45.0“,”onSale“:”false“,”货币“:”USD“,”currencyFormat“:”0.00“,”formattedListPrice“:”75.00“,”颜色描述“:”黑色/风帆/无烟煤“,”prdGroupId“:”943980“,”nikeType“:”鞋类“,”动态分布“:”includepromo“,”“GIFTS2014”、“nikeidmatchstyle”:“704691”、“宽度”:“常规”、“nikeidmatch”:“真实”、“发布日期”:“2010年11月5日”、“nikeidmatchproductid”:“1094199”、“部门代码”:“20”、“productdisplayorder”:“7”、“simplecolor”:“黑色”、“数量限制”:“2”、“GiftRap”:“真实”、“车型类型”:“鞋类”、“swatchcolorhex”:“000000”、“性别”:“男士”、“classid”:“120001”},”shipTo“:null,“giftMessage”:null,“giftMessageType”:0,“itemSource”:“SC”,“sizeType”:“displaySize”:“14”,“sizeDescription”:“14”,“eanNumber”:“00675911199978”,“colorNumber”:“010”,“colorDescription”:“Black/Sail/无烟煤”}});
哦!我都没注意到这是饼干的问题。谢谢!