gpt4 book ai didi

python - 在python中解析xml - 不理解DOM

转载 作者:行者123 更新时间:2023-11-30 23:31:28 27 4
gpt4 key购买 nike

我一整天都在阅读有关用 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 中的元素时,数据似乎按照我的预期结构化。

如果这被认为是完全愚蠢的,我深表歉意,提前致谢。

最佳答案

不幸的是,条形码在 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'])

关于python - 在python中解析xml - 不理解DOM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19893390/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com