【发布时间】:2013-11-22 11:44:57
【问题描述】:
我整天都在阅读有关使用 python 解析 xml 的内容,但是查看我需要提取数据的站点,我不确定我是否在找错树。基本上我想从超市网站(在图像名称中找到)获取 13 位条形码。例如:
http://www.tesco.com/groceries/SpecialOffers/SpecialOfferDetail/Default.aspx?promoId=A31033985
有 11 个项目和 11 个图像,第一个项目的条形码是 0000003235676。但是当我查看页面源时(我认为这是使用 python、urllib 和 beautifulsoup 一次性提取所有条形码的最佳方法)所有的条形码都在一行(第 12 行),但是数据似乎不像我所期望的那样在元素和属性方面结构化。
new TESCO.sites.UI.entities.Product({name:"Lb Mens Mattifying Dust 7G",xsiType:"QuantityOnlyProduct",productId:"275303365",baseProductId:"72617958",quantity:1,isPermanentlyUnavailable:true,imageURL:"http://img.tesco.com/Groceries/pi/805/5021320051805/IDShot_90x90.jpg",maxQuantity:99,maxGroupQuantity:0,bulkBuyLimitGroupId:"",increment:1,price:2.5,abbr:"g",unitPrice:3.58,catchWeight:"0",shelfName:"Mens Styling",superdepartment:"Health & Beauty",superdepartmentID:"TO_1448953606"});
new TESCO.sites.UI.entities.Product({name:"Lb Mens Thickening Shampoo 250Ml",xsiType:"QuantityOnlyProduct",productId:"275301223",baseProductId:"72617751",quantity:1,isPermanentlyUnavailable:true,imageURL:"http://img.tesco.com/Groceries/pi/225/5021320051225/IDShot_90x90.jpg",maxQuantity:99,maxGroupQuantity:0,bulkBuyLimitGroupId:"",increment:1,price:2.5,abbr:"ml",unitPrice:1,catchWeight:"0",shelfName:"Mens Shampoo ",superdepartment:"Health & Beauty",superdepartmentID:"TO_1448953606"});
new TESCO.sites.UI.entities.Product({name:"Lb Mens Sculpting Puty 75Ml",xsiType:"QuantityOnlyProduct",productId:"275301557",baseProductId:"72617906",quantity:1,isPermanentlyUnavailable:true,imageURL:"http://img.tesco.com/Groceries/pi/287/5021320051287/IDShot_90x90.jpg",maxQuantity:99,maxGroupQuantity:0,bulkBuyLimitGroupId:"",increment:1,price:2.5,abbr:"ml",unitPrice:3.34,catchWeight:"0",shelfName:"Pastes, Putty, Gums, Pomades",superdepartment:"Health & Beauty",superdepartmentID:"TO_1448953606"});
也许像 BeautifulSoup 这样的东西有点矫枉过正?我知道 DOM 树与原始源不同,但为什么它们如此不同 - 当我去检查 Firefox 中的元素时,数据似乎像我预期的那样结构化。
抱歉,如果这完全是愚蠢的,在此先感谢。
【问题讨论】:
-
在提到的页面上有 ID 为
h-278680465的元素。什么是条形码? -
这里没有要解析的 XML;您正在查看嵌入在 HTML 页面中的 JavaScript 源代码。
-
我认为您需要澄清您的问题是获取您显示的 javascript 位(这是一个 dom/html 解析问题),还是您需要弄清楚如何解析您显示的数据。
-
谢谢 Marcin,我认为我的问题是前者 - 抓取 Javascript,我认为其余的只是正则表达式。
标签: python xml parsing beautifulsoup