代码之家  ›  专栏  ›  技术社区  ›  iamericfletcher

解析JSON web输出

  •  0
  • iamericfletcher  · 技术社区  · 5 年前

    我正在使用以下网站上的requests和BeautifulSoup模块练习网络抓取:

    https://www.imdb.com/title/tt0080684/

    到目前为止,我的代码正确地输出了所讨论的json。我只想帮助从json中提取 name description 输入响应字典。

    # Send HTTP requests
    import requests
    
    import json
    
    from bs4 import BeautifulSoup
    
    
    class WebScraper:
    
        def send_http_request():
    
            # Obtain the URL via user input
            url = input('Input the URL:\n')
    
            # Get the webpage
            r = requests.get(url)
    
            soup = BeautifulSoup(r.content, 'html.parser')
    
            # Check response object's status code
            if r:
                p = json.loads("".join(soup.find('script', {'type':'application/ld+json'}).contents))
                print(p)
            else:
                print('\nInvalid movie page!')
    
    
    WebScraper.send_http_request()
    

    期望输出

    {"title": "Star Wars: Episode V - The Empire Strikes Back", "description": "After the Rebels are brutally overpowered by the Empire on the ice planet Hoth, Luke Skywalker begins Jedi training with Yoda, while his friends are pursued by Darth Vader and a bounty hunter named Boba Fett all over the galaxy."}
    
    2 回复  |  直到 5 年前
        1
  •  1
  •   GAP2002    5 年前

    您可以解析命令,然后使用 dumps 方法:

    # Send HTTP requests
    import requests
    
    import json
    
    from bs4 import BeautifulSoup
    
    
    class WebScraper:
    
        def send_http_request():
    
            # Obtain the URL via user input
            url = input('Input the URL:\n')
    
            # Get the webpage
            r = requests.get(url)
    
            soup = BeautifulSoup(r.content, 'html.parser')
    
            # Check response object's status code
            if r:
                p = json.loads("".join(soup.find('script', {'type':'application/ld+json'}).contents))
                output = json.dumps({"title": p["name"], "description": p["description"]})
                print(output)
            else:
                print('\nInvalid movie page!')
    
    
    WebScraper.send_http_request()
    

    {"title": "Star Wars: Episode V - The Empire Strikes Back", "description": "Star Wars: Episode V - The Empire Strikes Back is a movie starring Mark Hamill, Harrison Ford, and Carrie Fisher. After the Rebels are brutally overpowered by the Empire on the ice planet Hoth, Luke Skywalker begins Jedi training..."}
    
        2
  •  1
  •   Thang Pham    5 年前

    您只需要从中创建一个新词典 p 给我两把钥匙 name description .

            # Check response object's status code
            if r:
                p = json.loads("".join(soup.find('script', {'type':'application/ld+json'}).contents))
                desired_output = {"title": p["name"], "description": p["description"]}
                print(desired_output)
            else:
                print('\nInvalid movie page!')
    

    输出:

    {'title': 'Star Wars: Episode V - The Empire Strikes Back', 'description': 'Star Wars: Episode V - The Empire Strikes Back is a movie starring Mark Hamill, Harrison Ford, and Carrie Fisher. After the Rebels are brutally overpowered by the Empire on the ice planet Hoth, Luke Skywalker begins Jedi training...'}