如何搜索匹配的字符串然后提取它后面的字符串和冒号

时间:2017-04-29 17:55:12

标签: python regex web-scraping beautifulsoup pattern-matching

我是Python和网络抓取的新手,所以如果问题太基础,我道歉!

我想提取"得分"和"率" (评级)来自以下示例BeautifulSoup对象

import bs4
import re
text = '<html><body>{"count":1,"results":[{"score":"2-1","MatchId":{"number":"889349"},"name":"Match","rating":{"rate":9.0}}],"performance":{"comment":{}}}</body></html>'
page = bs4.BeautifulSoup(text, "lxml")
print type(page)

我试过这些,但没有出现(只是空白[])

tmp = page.find_all(text=re.compile("score:(.*)"));
print(tmp)

tmp = page.findAll("score");
print(tmp)

我发现了这个similar question,但它给了我错误

tmp = page.findAll(text = lambda(x): x.lower.index('score') != -1)
print(tmp)

AttributeError: 'builtin_function_or_method' object has no attribute 'index'

我做错了什么?提前谢谢!

1 个答案:

答案 0 :(得分:2)

这是turducken协议的三分之二。您可以使用beautifulsoup查找正文文本并使用json解码。然后你有一些python dicts和list to through。

>>> import json
>>> import bs4
>>> import re
>>> text = '<html><body>{"count":1,"results":[{"score":"2-1","MatchId":{"number":"889349"},"name":"Match","rating":{"rate":9.0}}],"performance":{"comment":{}}}</body></html>'
>>> page = bs4.BeautifulSoup(text, "lxml")
>>> 
>>> data = json.loads(page.find('body').text)
>>> for result in data["results"]:
...     print(result["score"], result["rating"]["rate"])
... 
2-1 9.0
>>>