从json源中抓取数据,为什么我只有1行?

时间:2019-02-25 10:58:30

标签: python json python-2.7 csv web-scraping

我正在尝试从网上商店的python网站获取一些信息。

我尝试了这个:

def proba():

    my_url = requests.get('https://www.telekom.hu/shop/categoryresults/?N=10994&contractType=list_price&instock_products=1&Ns=sku.sortingPrice%7C0%7C%7Cproduct.displayName%7C0&No=0&Nrpp=9&paymentType=FULL')
    data = my_url.json()
    results = []
    products = data['MainContent'][0]['contents'][0]['productList']['products']
    for product in products:
        name = product['productModel']['displayName']
        try:
            priceGross = product['priceInfo']['priceItemSale']['gross']
        except:
            priceGross = product['priceInfo']['priceItemToBase']['gross']
        url = product['productModel']['url']
        results.append([name, priceGross, url])
    df = pd.DataFrame(results, columns = ['Name', 'Price', 'Url'])    
# print(df)  ## print df
    df.to_csv(r'/usr/src/Python-2.7.13/test.csv', sep=',', encoding='utf-8-sig',index = False )

while True:
    mytime=datetime.now().strftime("%H:%M:%S")
    while mytime < "23:59:59":
    print mytime
    proba()
    mytime=datetime.now().strftime("%H:%M:%S")

在此网上商店中有9个项目,但我在csv文件中仅看到1行。

1 个答案:

答案 0 :(得分:1)

不能完全确定最终目标是什么。您要更新现有文件吗?一次性获取数据并全部写入?后面的示例如下所示,在该示例中,我将每个新数据帧添加到整体数据帧,并对函数调用使用Return语句以提供每个新数据帧。

SELECT V.I,
       sq.Descript
FROM (VALUES (1),(2),(3),(4),(5)) V (i)
     LEFT JOIN (SELECT TOP 5 
                       iw.Descript,
                       ROW_NUMBER() OVER (ORDER BY NEWID()) AS RN
                FROM staging.InputOrigin iw
                     LEFT JOIN [parameters].[VendorMasterUpdate] VMU ON VMU.article = iw.article
                                                                    AND iw.Country = VMU.origin
                WHERE post_advice = 'XXXXXXX'
                GROUP BY iw.Descript) sq ON sq.RN = V.i;