【问题标题】:Parsing xml in python - don't understand the DOM在 python 中解析 xml - 不了解 DOM
【发布时间】: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


【解决方案1】:

不幸的是,条形码在 HTML 中没有作为结构化数据给出;它仅作为 URL 的一部分嵌入。所以我们需要隔离 URL,然后通过字符串操作来提取条形码:

import urllib2
import bs4 as bs
import re
import urlparse

url = 'http://www.tesco.com/groceries/SpecialOffers/SpecialOfferDetail/Default.aspx?promoId=A31033985'

response = urllib2.urlopen(url)
content = response.read()
# with open('/tmp/test.html', 'w') as f:
#     f.write(content)
# Useful for debugging off-line:
# with open('/tmp/test.html', 'r') as f:
#     content = f.read()
soup = bs.BeautifulSoup(content)
barcodes = set()
for tag in soup.find_all('img', {'src': re.compile(r'/pi/')}):
    href = tag['src']
    scheme, netloc, path, query, fragment = urlparse.urlsplit(href)
    barcodes.add(path.split('\\')[1])

print(barcodes)

产量

set(['0000003222737', '0000010039670', '0000010036297', '0000010008393', '0000003050453', '0000010062951', '0000003239438', '0000010078402', '0000010016312', '0000003235676', '0000003203132'])

【讨论】:

  • 我应该指定条形码是 13 位数字,所以在 img.tesco.com/Groceries/pi/676/0000003235676/…> 是0000003235676 位。我希望它与 productId 位于同一个位置,并且正在努力寻找像您提供的答案,无论如何都干杯!
  • 好的,我已经编辑了帖子;技术基本相同。
  • 这太棒了,非常感谢! (虽然我觉得我被骗了哈哈)
【解决方案2】:

由于您的网站使用 javascript 来格式化其内容,您可能会发现从 urllib 切换到像 Selenium 这样的工具很有用。这样,您可以在页面呈现给使用 Web 浏览器的真实用户时抓取页面。这个github project 似乎可以解决你的任务。

其他选项将从页面 javascript 脚本中过滤掉 json 数据并直接从那里获取数据。

【讨论】:

  • 谢谢,这看起来很有希望。
猜你喜欢
  • 2010-12-25
  • 1970-01-01
  • 1970-01-01
  • 2014-05-19
  • 1970-01-01
  • 2016-02-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多