【问题标题】:How to get consistency output from inconsistency如何从不一致中获得一致性输出
【发布时间】:2021-12-31 10:28:28
【问题描述】:

我想一次获取所有 name 和 price 键,但所有第一个 dict 键的顺序不同,我无法迭代,这就是为什么我必须进行一些预处理但输出不一致的原因。

电流输出:

                        Name                                              Price
0  Half-Life: Opposing Force  [{'id': 32, 'discount_block': '<div class="dis...
1                  Half-Life  [{'id': 34, 'discount_block': '<div class="dis...
2                Half-Life 2  [{'id': 36, 'discount_block': '<div class="dis...
3   Half-Life 2: Episode Two  [{'id': 516, 'discount_block': '<div class="di...
4                    Cuphead  [{'id': 35659, 'discount_block': '<div class="...
5           Steam Controller                                                 []
6                  PCMark 10  [{'id': 125001, 'discount_block': '<div class=...
7     Kerbal Space Program 2                                                 []
8    Hollow Knight: Silksong 

预期输出:

               name           price
0 Half-Life: Opposing Force     59
1                  Half-Life    109
2                Half-Life 2    109
3   Half-Life 2: Episode Two    89
4                    Cuphead    573
5           Steam Controller    []# meaning None
6                  PCMark 10    157
7     Kerbal Space Program 2    []
8    Hollow Knight: Silksong    []

脚本:

import re
import json
import requests
import pandas as pd

url = 'https://store.steampowered.com/wishlist/id/zorro4/#sort=order'
wishlist_url =  json.loads( re.findall(r'g_strWishlistBaseURL = (".*?");', requests.get(url).text)[0] )
#print(wishlist_url)

data = requests.get(wishlist_url + 'wishlistdata/?p=0').json()

#print(wishlist_url + 'wishlistdata/?p=0')

# jsn_data=json.dumps(data, indent=4)
# with open('da.json','w') as f:
#     f.write(jsn_data)
names = [d['name'] for d in data.values()]
# print(names)

out = list(map(lambda x: x['subs'], data.values()))
p=[]
for i in out:
    for t in i:
        q=t['price']
        p.append(q)
        #print(q)

df = pd.DataFrame(data=list(zip(names, out)), columns=['Name', 'Price'])
print(df)

【问题讨论】:

    标签: python python-3.x pandas web-scraping beautifulsoup


    【解决方案1】:

    遍历data,您可以在list 中提取数据 使用index 位置,它给出html 标签现在使用bs4 查找 到目前为止,我已经采取了discount_final_price 的具体价格 输出。

    并将您的数据附加到lst,以便返回值列表和 使用pandas 创建df 并将数据作为lst 和列作为您 想要

    import numpy as np
    lst=[]
    for key,value in data.items():
        try:
            name=value['name']
            price_data=value['subs'][0]['discount_block']
            soup=BeautifulSoup(price_data,"html.parser")
            price=soup.find("div",class_="discount_final_price").get_text().split(" ")[-1]
        except:
            price=np.nan
        lst.append([name,price])
    

    输出:

                    name          price
    0   Half-Life: Opposing Force   39
    1   Half-Life                   69
    2   Half-Life 2                 69
    3   Half-Life 2: Episode Two    59
    4   Cuphead                     395
    5   Steam Controller            NaN
    6   PCMark 10                   104
    7   Kerbal Space Program 2      NaN
    8   Hollow Knight: Silksong     NaN
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-04
      • 2019-11-07
      • 2018-04-30
      • 2013-04-15
      相关资源
      最近更新 更多