【问题标题】:Struggling to scrape Pandora Jewellery Stores?苦苦挣扎潘多拉珠宝店?
【发布时间】:2019-10-07 00:32:15
【问题描述】:

我试图使用 Parsehub 为澳大利亚的所有地点及其地址抓取 https://stores.pandora.net/en-au/,但它并没有像往常一样抛出结果。

解析中心截图:

如图所示,实时预览显示表格非常好,但是当我运行它时,它只会抛出垃圾值(比如美国的 2 家商店)

我尝试过使用 Beautiful soup,但课程看起来比我最初想象的要复杂。 (看起来它位于 Maplist 数组中,但我不确定如何提取该位)

这里的任何帮助将不胜感激!谢谢:)

【问题讨论】:

  • 听起来他们的目标是 IP 地址的地理位置。

标签: jquery web-scraping beautifulsoup parsehub


【解决方案1】:

此站点从该 API https://maps.pandora.net/api/getAsyncLocations 获取数据,查询参数中带有 search 值。结果是一个带有字段maplist 的 JSON 对象,其中包含 html 数据(单个 div)。这个 div 嵌入了几个以逗号分隔的 JSON 对象:

curl 'https://maps.pandora.net/api/getAsyncLocations?level=domain&template=domain&search=Melbourne+Victoria%2C+Australie'

所以我们需要将逗号分隔的 JSON 对象重新排列成一个数组来解析它。以下示例使用(json 解析器)、(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) 
    }'

中:

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
])

【讨论】:

    猜你喜欢
    • 2015-05-06
    • 1970-01-01
    • 1970-01-01
    • 2014-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多