python
  • web
  • beautifulsoup
  • screen-scraping
  • 2017-09-16 107 views 2 likes 
    2

    我是Python的新手,我正在使用BeautifulSoup编写Python中的一个小刮板,以便从网页获取地址。我重视的是 enter image description here如何使用BeautifulSoup获取Python中的特定内容?

    </div> 
        </div> 
        <div data-integration-name="redux-container" data-payload='{"name":"LocationsMapList","props":{"locations":[{"id":17305,"company_id":106906,"description":"","city":"New York","country":"United States","address":"5 Crosby St 3rd Floor","state":"New York","region":"","latitude":40.719753,"longitude":-74.0001954,"hq":true,"created_at":"2015-01-19T01:32:16.317Z","updated_at":"2016-05-05T07:57:19.282Z","zip_code":"10013","country_code":"US","full_address":"5 Crosby St 3rd Floor, New York, 10013, New York, USA","dirty":false,"to_params":"new-york-us"}]},"storeName":null}' data-rwr-element="true"> 
    

    我使用BeautifulSoup了全部内容的图片,但我不知道如何提取“full_address”的内容。我看到它在“div”中,但我不知道下一步该怎么做。

    links = soup.find_all('div')

    非常感谢!

    +3

    (请添加您的代码作为文本,而不是图片) – PRMoureu

    +1

    我加入吧。谢谢! – Laura

    +0

    'data-payload''属性是json,所以使用'json.loads' –

    回答

    2

    您可以使用json分析数据:

    #!/usr/bin/env python 
    
    from bs4 import BeautifulSoup 
    import json 
    
    data = ''' 
    </div> 
        </div> 
        <div data-integration-name="redux-container" data-payload='{"name":"LocationsMapList","props":{"locations":[{"id":17305,"company_id":106906,"description":"","city":"New York","country":"United States","address":"5 Crosby St 3rd Floor","state":"New York","region":"","latitude":40.719753,"longitude":-74.0001954,"hq":true,"created_at":"2015-01-19T01:32:16.317Z","updated_at":"2016-05-05T07:57:19.282Z","zip_code":"10013","country_code":"US","full_address":"5 Crosby St 3rd Floor, New York, 10013, New York, USA","dirty":false,"to_params":"new-york-us"}]},"storeName":null}' data-rwr-element="true"> 
    ''' 
    
    soup = BeautifulSoup(data, 'html.parser') 
    for i in soup.find_all('div', attrs={'data-integration-name':'redux-container'}): 
        info = json.loads(i.get('data-payload')) 
        for i in info['props']['locations']: 
         print i['address'] 
    
    +0

    它说:KeyError:'locations' – Laura

    +1

    @Laura为我的解决方案工作我假设你试图解析的数据和你的文章中的数据完全一样。您的数据是否与您发布的错误具有相同的原因,似乎数据中不存在“位置”?同样为了查看键值,你可以'print i.keys()' – coder

    +0

    也许将它限制为''div's''data-integration-name =“redux-container”''属性。 – wwii

    相关问题