用 Beautiful Soup 解析 HTML 中的 Data-Bind 标签
Posted
技术标签:
【中文标题】用 Beautiful Soup 解析 HTML 中的 Data-Bind 标签【英文标题】:Parsing Data-Bind tag within HTML with Beautiful Soup 【发布时间】:2016-12-26 03:28:00 【问题描述】:我无法在 Beautiful Soup 中选择这个“div”对象,然后解析其中的数据。
首先我必须解码 html 实体,例如本网站 (https://mothereff.in/html-entities) 上的函数。
我将采取哪些步骤,例如,以编程方式选择
(extraLarge:'/s3/fhphotos/CIRD-72K6-H9_SID_1.jpg,width=1000&height=1000&mode=max')
从下面的代码
<div data-bind="component: name: 'product-detail', params: hasVariants:true,name:'***S LOUNGE CHAIR',hasCategory:true,superCategoryName:'Furniture',categoryDisplayName:'Living Room',categorySlug:'living-room',subcategoryDisplayName:'Chairs',subcategorySlug:'chairs',collection:id:1529,name:'Irondale',description:'Each piece is a striking conversation-starter. Tables are made from reclaimed doors paired with salvaged architecture or old machine parts. Storage solutions are inspired by libraries of the 1940’s. Cast iron beds with linen panels as well as seating in linen, lush velvet and top-grain leather offer a distinctive found feel.',isFeatured:true,isNew:false,image:'/FourHandsMarketplace/media/General/Featured%20Collections/IRONDALE.jpg?width=500',shortDescription:'Moving from Parisian flea market to modern to industrial, understated elegance is a common theme. Waxed leathers and distressed irons mix with fabrics for an intriguing style blend.\r\n',uri:'/collections/irondale',attributes:[id:384,name:'COVER',displayOrder:30,swatches:true,values:[id:12710,name:'EBONY',displayOrder:1,swatchUrl:'/s3/fhphotos/Y C11458-G6_PRM_1.jpg?width=200&height=200&mode=crop',id:12711,name:'STONEWASH DARK GREEN',displayOrder:2,swatchUrl:'/s3/fhphotos/Y C11458-H9_PRM_1.jpg?width=200&height=200&mode=crop'],id:385,name:'FINISH',displayOrder:40,swatches:true,values:[id:12712,name:'BLACK WASH WEATHERED',displayOrder:1,swatchUrl:'/s3/fhphotos/Y C11458-K5_PRM_1.jpg?width=200&height=200&mode=crop',id:12713,name:'DISTRESSED WASHED OLD OAK',displayOrder:2,swatchUrl:'/s3/fhphotos/Y C11458-K6_PRM_1.jpg?width=200&height=200&mode=crop']],products:[attributeValueIds:[12710,12712],description:'Our take on the classic Adirondack emphasizes comfort with thick, top-grain leather cushioning. Wire-brushed oak is finished in black and hand-distressed for a naturally weathered patina.',dimensions:'W: 27.75" H: 29" D: 34.75"',availabilityDescription:'<strong>Quantity in Stock: </strong><span >88</span><br /><strong>More on the Way: </strong><span >Yes</span><br /><strong>Estimated Arrival Date: </strong><span >1 to 2 weeks</span>',colors:['Black Washed Weathered','Ebony'],weightPounds:45.0,volumeCubicFeet:18.72,images:[order:1,thumb:'/s3/fhphotos/CIRD-72K5-G6H6_PRM_1.jpg?width=200&height=200&mode=crop',large:'/s3/fhphotos/CIRD-72K5-G6H6_PRM_1.jpg?width=600&height=600&mode=max',extraLarge:'/s3/fhphotos/CIRD-72K5-G6H6_PRM_1.jpg?width=1000&height=1000&mode=max',full:'http://fhphotos.s3-website-us-east-1.amazonaws.com/CIRD-72K5-G6H6_PRM_1.jpg',order:2,thumb:'/s3/fhphotos/CIRD-72K5-G6H6_ROM_1.jpg?width=200&height=200&mode=crop',large:'/s3/fhphotos/CIRD-72K5-G6H6_ROM_1.jpg?width=600&height=600&mode=max',extraLarge:'/s3/fhphotos/CIRD-72K5-G6H6_ROM_1.jpg?width=1000&height=1000&mode=max',full:'http://fhphotos.s3-website-us-east-1.amazonaws.com/CIRD-72K5-G6H6_ROM_1.jpg',order:3,thumb:'/s3/fhphotos/CIRD-72K5-G6H6_ROM_2.jpg?width=200&height=200&mode=crop',large:'/s3/fhphotos/CIRD-72K5-G6H6_ROM_2.jpg?width=600&height=600&mode=max',extraLarge:'/s3/fhphotos/CIRD-72K5-G6H6_ROM_2.jpg?width=1000&height=1000&mode=max',full:'http://fhphotos.s3-website-us-east-1.amazonaws.com/CIRD-72K5-G6H6_ROM_2.jpg',order:4,thumb:'/s3/fhphotos/CIRD-72K5-G6H6_DET_1.jpg?width=200&height=200&mode=crop',large:'/s3/fhphotos/CIRD-72K5-G6H6_DET_1.jpg?width=600&height=600&mode=max',extraLarge:'/s3/fhphotos/CIRD-72K5-G6H6_DET_1.jpg?width=1000&height=1000&mode=max',full:'http://fhphotos.s3-website-us-east-1.amazonaws.com/CIRD-72K5-G6H6_DET_1.jpg',order:5,thumb:'/s3/fhphotos/CIRD-72K5-G6H6_DET_2.jpg?width=200&height=200&mode=crop',large:'/s3/fhphotos/CIRD-72K5-G6H6_DET_2.jpg?width=600&height=600&mode=max',extraLarge:'/s3/fhphotos/CIRD-72K5-G6H6_DET_2.jpg?width=1000&height=1000&mode=max',full:'http://fhphotos.s3-website-us-east-1.amazonaws.com/CIRD-72K5-G6H6_DET_2.jpg',order:6,thumb:'/s3/fhphotos/CIRD-72K5-G6H6_BCK_1.jpg?width=200&height=200&mode=crop',large:'/s3/fhphotos/CIRD-72K5-G6H6_BCK_1.jpg?width=600&height=600&mode=max',extraLarge:'/s3/fhphotos/CIRD-72K5-G6H6_BCK_1.jpg?width=1000&height=1000&mode=max',full:'http://fhphotos.s3-website-us-east-1.amazonaws.com/CIRD-72K5-G6H6_BCK_1.jpg',order:7,thumb:'/s3/fhphotos/CIRD-72K5-G6H6_FRT_1.jpg?width=200&height=200&mode=crop',large:'/s3/fhphotos/CIRD-72K5-G6H6_FRT_1.jpg?width=600&height=600&mode=max',extraLarge:'/s3/fhphotos/CIRD-72K5-G6H6_FRT_1.jpg?width=1000&height=1000&mode=max',full:'http://fhphotos.s3-website-us-east-1.amazonaws.com/CIRD-72K5-G6H6_FRT_1.jpg',order:8,thumb:'/s3/fhphotos/CIRD-72K5-G6H6_SID_1.jpg?width=200&height=200&mode=crop',large:'/s3/fhphotos/CIRD-72K5-G6H6_SID_1.jpg?width=600&height=600&mode=max',extraLarge:'/s3/fhphotos/CIRD-72K5-G6H6_SID_1.jpg?width=1000&height=1000&mode=max',full:'http://fhphotos.s3-website-us-east-1.amazonaws.com/CIRD-72K5-G6H6_SID_1.jpg',order:9,thumb:'/s3/fhphotos/CIRD-72K5-G6H6_ROM_3.jpg?width=200&height=200&mode=crop',large:'/s3/fhphotos/CIRD-72K5-G6H6_ROM_3.jpg?width=600&height=600&mode=max',extraLarge:'/s3/fhphotos/CIRD-72K5-G6H6_ROM_3.jpg?width=1000&height=1000&mode=max',full:'http://fhphotos.s3-website-us-east-1.amazonaws.com/CIRD-72K5-G6H6_ROM_3.jpg',order:10,thumb:'/s3/fhphotos/CIRD-72K5-G6H6_DET_3.jpg?width=200&height=200&mode=crop',large:'/s3/fhphotos/CIRD-72K5-G6H6_DET_3.jpg?width=600&height=600&mode=max',extraLarge:'/s3/fhphotos/CIRD-72K5-G6H6_DET_3.jpg?width=1000&height=1000&mode=max',full:'http://fhphotos.s3-website-us-east-1.amazonaws.com/CIRD-72K5-G6H6_DET_3.jpg',order:11,thumb:'/s3/fhphotos/CIRD-72K5-G6H6_ROM_4.jpg?width=200&height=200&mode=crop',large:'/s3/fhphotos/CIRD-72K5-G6H6_ROM_4.jpg?width=600&height=600&mode=max',extraLarge:'/s3/fhphotos/CIRD-72K5-G6H6_ROM_4.jpg?width=1000&height=1000&mode=max',full:'http://fhphotos.s3-website-us-east-1.amazonaws.com/CIRD-72K5-G6H6_ROM_4.jpg'],priceHtml:'$520.00',itemNumber:'CIRD-72K5-G6H6',name:'***s Lounge Chair-Ebony, Blk Wsh Weath',availableForImmediateShipment:true,isNew:false,isCloseout:false,attributeValueIds:[12711,12713],description:'Our take on the classic Adirondack emphasizes comfort with green, stonewashed cotton canvas cushioning. Wire-brushed oak is hand-distressed for a naturally weathered patina.',dimensions:'W: 27.75" H: 29" D: 34.5"',availabilityDescription:'<strong>Quantity in Stock: </strong><span >147</span><br /><strong>More on the Way: </strong><span >Yes</span><br /><strong>Estimated Arrival Date: </strong><span >1 to 2 weeks</span>',colors:['Distressed Washed Old Oak','Stonewash Dark Green'],weightPounds:45.0,volumeCubicFeet:18.72,images:[order:1,thumb:'/s3/fhphotos/CIRD-72K6-H9_PRM_1.jpg?width=200&height=200&mode=crop',large:'/s3/fhphotos/CIRD-72K6-H9_PRM_1.jpg?width=600&height=600&mode=max',extraLarge:'/s3/fhphotos/CIRD-72K6-H9_PRM_1.jpg?width=1000&height=1000&mode=max',full:'http://fhphotos.s3-website-us-east-1.amazonaws.com/CIRD-72K6-H9_PRM_1.jpg',order:2,thumb:'/s3/fhphotos/CIRD-72K6-H9_ROM_1.jpg?width=200&height=200&mode=crop',large:'/s3/fhphotos/CIRD-72K6-H9_ROM_1.jpg?width=600&height=600&mode=max',extraLarge:'/s3/fhphotos/CIRD-72K6-H9_ROM_1.jpg?width=1000&height=1000&mode=max',full:'http://fhphotos.s3-website-us-east-1.amazonaws.com/CIRD-72K6-H9_ROM_1.jpg',order:3,thumb:'/s3/fhphotos/CIRD-72K6-H9_ROM_2.jpg?width=200&height=200&mode=crop',large:'/s3/fhphotos/CIRD-72K6-H9_ROM_2.jpg?width=600&height=600&mode=max',extraLarge:'/s3/fhphotos/CIRD-72K6-H9_ROM_2.jpg?width=1000&height=1000&mode=max',full:'http://fhphotos.s3-website-us-east-1.amazonaws.com/CIRD-72K6-H9_ROM_2.jpg',order:4,thumb:'/s3/fhphotos/CIRD-72K6-H9_DET_1.jpg?width=200&height=200&mode=crop',large:'/s3/fhphotos/CIRD-72K6-H9_DET_1.jpg?width=600&height=600&mode=max',extraLarge:'/s3/fhphotos/CIRD-72K6-H9_DET_1.jpg?width=1000&height=1000&mode=max',full:'http://fhphotos.s3-website-us-east-1.amazonaws.com/CIRD-72K6-H9_DET_1.jpg',order:5,thumb:'/s3/fhphotos/CIRD-72K6-H9_DET_2.jpg?width=200&height=200&mode=crop',large:'/s3/fhphotos/CIRD-72K6-H9_DET_2.jpg?width=600&height=600&mode=max',extraLarge:'/s3/fhphotos/CIRD-72K6-H9_DET_2.jpg?width=1000&height=1000&mode=max',full:'http://fhphotos.s3-website-us-east-1.amazonaws.com/CIRD-72K6-H9_DET_2.jpg',order:6,thumb:'/s3/fhphotos/CIRD-72K6-H9_BCK_1.jpg?width=200&height=200&mode=crop',large:'/s3/fhphotos/CIRD-72K6-H9_BCK_1.jpg?width=600&height=600&mode=max',extraLarge:'/s3/fhphotos/CIRD-72K6-H9_BCK_1.jpg?width=1000&height=1000&mode=max',full:'http://fhphotos.s3-website-us-east-1.amazonaws.com/CIRD-72K6-H9_BCK_1.jpg',order:7,thumb:'/s3/fhphotos/CIRD-72K6-H9_FRT_1.jpg?width=200&height=200&mode=crop',large:'/s3/fhphotos/CIRD-72K6-H9_FRT_1.jpg?width=600&height=600&mode=max',extraLarge:'/s3/fhphotos/CIRD-72K6-H9_FRT_1.jpg?width=1000&height=1000&mode=max',full:'http://fhphotos.s3-website-us-east-1.amazonaws.com/CIRD-72K6-H9_FRT_1.jpg',order:8,thumb:'/s3/fhphotos/CIRD-72K6-H9_SID_1.jpg?width=200&height=200&mode=crop',large:'/s3/fhphotos/CIRD-72K6-H9_SID_1.jpg?width=600&height=600&mode=max',extraLarge:'/s3/fhphotos/CIRD-72K6-H9_SID_1.jpg?width=1000&height=1000&mode=max',full:'http://fhphotos.s3-website-us-east-1.amazonaws.com/CIRD-72K6-H9_SID_1.jpg',order:9,thumb:'/s3/fhphotos/CIRD-72K6-H9_ROM_3.jpg?width=200&height=200&mode=crop',large:'/s3/fhphotos/CIRD-72K6-H9_ROM_3.jpg?width=600&height=600&mode=max',extraLarge:'/s3/fhphotos/CIRD-72K6-H9_ROM_3.jpg?width=1000&height=1000&mode=max',full:'http://fhphotos.s3-website-us-east-1.amazonaws.com/CIRD-72K6-H9_ROM_3.jpg',order:10,thumb:'/s3/fhphotos/CIRD-72K6-H9_DET_3.jpg?width=200&height=200&mode=crop',large:'/s3/fhphotos/CIRD-72K6-H9_DET_3.jpg?width=600&height=600&mode=max',extraLarge:'/s3/fhphotos/CIRD-72K6-H9_DET_3.jpg?width=1000&height=1000&mode=max',full:'http://fhphotos.s3-website-us-east-1.amazonaws.com/CIRD-72K6-H9_DET_3.jpg'],priceHtml:'$290.00',itemNumber:'CIRD-72K6-H9',name:'***s Lounge Chair-Stonewsh Drk Green',availableForImmediateShipment:true,isNew:false,isCloseout:false],activeItemNumber:'CIRD-72K5-G6H6',priceDescription:'Wholesale Price' "></div>
?
【问题讨论】:
【参考方案1】:目前还不完全清楚这个 html 字符串的来源以及您对提取什么感兴趣,但对于 Beautiful Soup 部分,您只需要:
soup = BeautifulSoup(s)
text = soup.div['data-bind']
其中 s 是您问题中的字符串。我们首先获取 'div' tag,然后再获取 'data-bind' attribute。
这种格式让我感到困惑,因为它类似于 json 并且类似于 python 字典,但是这些解析器都不喜欢输入。我猜它的javascript?我受question 的启发编写了一个快速而肮脏的括号计数循环:
nest_lvl = 0
lvl_string = list()
for char in text:
if char == '':
nest_lvl += 1
elif char == '':
nest_lvl -= 1
try:
lvl_string[nest_lvl] += char
except IndexError: # first iter
lvl_string.append(char)
if char == '':
print nest_lvl, lvl_string[nest_lvl]
lvl_string[nest_lvl] = ''
希望能帮助您入门。同样,解析部分实际上取决于解析器需要的通用性以及您要提取的确切内容。
【讨论】:
以上是关于用 Beautiful Soup 解析 HTML 中的 Data-Bind 标签的主要内容,如果未能解决你的问题,请参考以下文章