如何使用BeautifulSoup在Python中获取特定内容?

时间:2017-09-16 21:32:06

标签: python web beautifulsoup screen-scraping

我是Python的新手,我使用BeautifulSoup在Python中编写了一个小刮刀,以便从网页上获取地址。我附上了它的图片 enter image description here

    </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获得了全部内容,但我不知道如何提取&#34; full_address&#34;的内容。我在&#34; div&#34;中看到了它。但我不知道接下来该做什么。

links = soup.find_all('div')

非常感谢!

1 个答案:

答案 0 :(得分: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']