python 将循环刮到pd数据帧

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 将循环刮到pd数据帧相关的知识,希望对你有一定的参考价值。

#Joe says to try and except every time

import pandas as pd
dc_eats = pd.DataFrame(columns=["name","location","price","bookings"])

# loop through each entry
for entry in html.find_all('div', {'class':'result content-section-list-row cf with-times'}):
    # grab the name
    name = entry.find('span', {'class': 'rest-row-name-text'}).text
    # grab the location
    location = entry.find('span', {'class': 'rest-row-meta--location rest-row-meta-text'}).text
    # grab the price
    price = entry.find('div', {'class': 'rest-row-pricing'}).find('i').text.count('$') #can chain
    # try to find the number of bookings
    try:
        temp = entry.find('div', {'class':'booking'}).text
        match = re.search(r'\d+', temp)
        if match:
            bookings = match.group()
    except:
        bookings = 0
    # add to df
    dc_eats.loc[len(dc_eats)]=[name, location, price, bookings]

以上是关于python 将循环刮到pd数据帧的主要内容,如果未能解决你的问题,请参考以下文章

循环遍历多个 panda 数据帧以在 Python 中获取多个数据帧输出

将 Python3 中的数据帧写入 Netezza

如何使用循环将多个条件应用于数据帧

在循环中连接数据帧非常慢

python 将所有html选择标签项目刮到网站上

将操作应用于字典中的所有数据帧,无需循环