我试图使用Parsehub抓取澳大利亚所有位置及其地址的https://stores.pandora.net/en-au/,但它并没有像往常那样抛出结果。
解析集线器屏幕截图:

如图所示,实时预览显示的表格非常好,但当我运行它时,它只抛出垃圾值(比如美国的2个商店)。
我尝试过使用Beautiful soup,但类看起来比我最初想象的要复杂得多。(看起来它位于Maplist数组中,但我不确定如何提取它)
这里的任何帮助都将不胜感激!谢谢:)
发布于 2019-10-08 07:45:26
此站点通过查询参数中的search值从此接口https://maps.pandora.net/api/getAsyncLocations获取数据。结果是一个带有字段maplist的JSON对象,其中包含html数据(单个div)。此div嵌入了几个以逗号分隔的JSON对象:
curl 'https://maps.pandora.net/api/getAsyncLocations?level=domain&template=domain&search=Melbourne+Victoria%2C+Australie'因此,我们需要将逗号分隔的JSON对象重新排列到一个数组中,以便对其进行解析。下面的示例使用curl、jq (json解析器)、sed & pup (html解析器)来提取数据:
search="Melbourne+Victoria+Australie"
curl -s -G 'https://maps.pandora.net/api/getAsyncLocations' \
-d 'level=domain' \
-d 'template=domain' \
-d "search=$search" | \
jq -r '.maplist' | \
pup -p div text{} | \
sed '$ s/.$//' | \
sed -e "\$a]" | \
sed '1s/^/[/' | \
jq '.[] | {
location: .location_name,
address: .address_1,
complement: (.city + "," + .big_region + " " + .location_post_code)
}'在带有python-requests和beautifulsoup的python中:
import requests
from bs4 import BeautifulSoup
import json
search = "Melbourne+Victoria+Australie"
response = requests.get(
'https://maps.pandora.net/api/getAsyncLocations',
params = {
'level':'domain',
'template':'domain',
'search': search
}
)
soup = BeautifulSoup(response.json()['maplist'], 'html.parser')
formatted_json = "[{}]".format(soup.div.string[:-1])
data = json.loads(formatted_json)
print([
(i['location_name'], i['address_1'], i['city'], i['big_region'], i['location_post_code'])
for i in data
])https://stackoverflow.com/questions/58262321
复制相似问题