【问题标题】:How to re.search html source with regex in python如何在 python 中使用正则表达式重新搜索 html 源代码
【发布时间】:2010-12-22 20:41:52
【问题描述】:

我脑子坏了,但无法解决这个问题。

数字,名字是虚构的。但是思路是这样的

我阅读了类似“https://graph.facebook.com/123”之类的链接

这个结果到源代码:

{
   "id": "123",
   "name": "John Doe",
   "first_name": "John",
   "last_name": "Doe",
   "link": "http://www.facebook.com/people/John-Doe/123",
   "gender": "male",
   "locale": "en_US"
}

我想提取id、name等所有信息

我试试这个,但失败了

    link = 'https://graph.facebook.com/123'
    result = browser.open(link)
    text = result.read()
    result.close()
    id = re.search('"id": "(.*?)",', cont)

regex '"id": "(.*?)",' 似乎是正确的,但没有返回任何内容.. 为什么???

【问题讨论】:

    标签: python regex json


    【解决方案1】:

    这似乎是 JSON,你不想使用正则表达式来解析它。

    link = 'https://graph.facebook.com/123'
    result = browser.open(link)
    data = json.load(result)
    print data['id']
    

    【讨论】:

    • @user492741,你应该点击答案旁边的复选标记然后接受它。
    猜你喜欢
    • 2012-05-02
    • 2012-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-29
    • 1970-01-01
    • 2011-11-16
    • 1970-01-01
    相关资源
    最近更新 更多