gpt4 book ai didi

python - 如何用BeautifulSoup获取Python中的具体内容?

转载 作者:太空宇宙 更新时间:2023-11-04 02:41:52 24 4
gpt4 key购买 nike

我是 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 获得了完整的内容,但我不知道如何提取“full_address”的内容。我看到它在“div”中,但我不知道下一步该做什么。

links = soup.find_all('div')

非常感谢!

最佳答案

您可以使用 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']

关于python - 如何用BeautifulSoup获取Python中的具体内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46258597/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com