【发布时间】:2017-11-04 06:46:50
【问题描述】:
我正在解析这段 HTML
<script type="text/javascript">
var spConfig = new Product.Config({"attributes":{"150":{"id":"150","code":"size_shoe","label":"Schuhgr\u00f6\u00dfe","options":[{"id":"494","label":"36","price":"0","oldPrice":"0","products":["393318"],"label_us":"\r\n4Y","label_uk":"3.5","label_cm":"23","label_int":null},{"id":"476","label":"36.5","price":"0","oldPrice":"0","products":["393321"],"label_us":"\r\n4.5Y","label_uk":"4","label_cm":"23.5","label_int":null,"out_of_stock":"Out of Stock"},{"id":"130","label":"37.5","price":"0","oldPrice":"0","products":["393324"],"label_us":"\r\n5Y","label_uk":"4.5","label_cm":"23.5","label_int":null,"out_of_stock":"Out of Stock"},{"id":"12","label":"38","price":"0","oldPrice":"0","products":["393327"],"label_us":"\r\n5.5Y","label_uk":"5","label_cm":"24","label_int":null},{"id":"500","label":"38.5","price":"0","oldPrice":"0","products":["393330"],"label_us":"\r\n6Y","label_uk":"5.5","label_cm":"24","label_int":null,"out_of_stock":"Out of Stock"},{"id":"10","label":"40","price":"0","oldPrice":"0","products":["393333"],"label_us":"\r\n7Y","label_uk":"6","label_cm":"25","label_int":null,"out_of_stock":"Out of Stock"}]}},"template":"\u20ac#{price}","basePrice":"89.95","oldPrice":"89.95","productId":"393306","chooseText":"Choose an Option...","taxConfig":{"includeTax":true,"showIncludeTax":true,"showBothPrices":false,"defaultTax":19,"currentTax":19,"inclTaxTitle":"Incl. Tax"}});
</script>
这是我的代码
import urllib2, requests, json, re
from bs4 import BeautifulSoup as bs
url = ("link")
session = requests.session()
response = session.get(url)
soup = bs(response.text, 'html.parser')
scripts = soup.findAll('script')
def getIds():
for script in scripts:
if 'spConfig =' in script.getText():
regex = re.compile(r'var spConfig = new Product.Config\((.*?)\);')
match = regex.search(script.getText())
spConfig = json.loads(match.groups()[0])
for key in spConfig['attributes']: # Should only call once
for product in spConfig['attributes'][key]['options']:
if product['label_us']:
size_id = product['id']
product_id = spConfig['attributes'][key]['id']
print product_id
getIds()
我的输出是
150
150
150
150
150
150
150
150
150
150
150
150
150
150
150
150
我要解析并得到脚本输出的是;
393318 - 4.Y
393321 - 4.5Y - Out of Stock
393324 - 5Y - Out of Stock
等等等等
我错误地解析了哪些属性?另外,如我的示例所示,如何使用文本大小格式化输出并在需要时包括“缺货”?
【问题讨论】:
-
你检查过 HTMLParser 吗? docs.python.org/2/library/htmlparser.html
标签: javascript python html parsing beautifulsoup