gpt4 book ai didi

python - 将网络抓取结果加载到 Pandas DataFrame 中

转载 作者:太空宇宙 更新时间:2023-11-04 04:57:59 25 4
gpt4 key购买 nike

我有以下代码:

sauce = urllib.request.urlopen('https://www.iproperty.com.my/sale/selangor/all-commercial/?q=UOA%20Business%20Park').read()
soup = bs.BeautifulSoup(sauce,'html.parser')

price = soup.find_all('ul',class_='listing-primary-price jMWEse')

BUA = soup.find_all('li',class_='attributes-price-per-unit-size-item builtUp-attr fsbnan')


for data in price:
Price = data.text
print(Price)

for data in BUA:
BUA = data.text
print(BUA)

打印 PriceBUA 得到以下结果:

Price:
RM 1,067,490
RM 2,246,160
RM 929,160
RM 1,321,000
RM 103,840,000

BUA:
Built-up : 1,227 sq. ft.Built-up : 1,227 sq. ft.
Built-up : 2,292 sq. ft.Built-up : 2,292 sq. ft.
Built-up : 1,044 sq. ft.Built-up : 1,044 sq. ft.
Built-up : 1,335 sq. ft.Built-up : 1,335 sq. ft.
Built-up : 118,000 sq. ft.Built-up : 118,000 sq. ft.

我的问题是,如何将 PriceBUA 加载到 Pandas Dataframe 中,因为我想加入它们并打印最终结果,例如:

    Price:              BUA:        
0 RM 1,067,490 Built-up : 1,227 sq. ft.Built-up : 1,227 sq. ft.
1 RM 2,246,160 Built-up : 2,292 sq. ft.Built-up : 2,292 sq. ft.
2 RM 929,160 Built-up : 1,044 sq. ft.Built-up : 1,044 sq. ft.
3 RM 1,321,000 Built-up : 1,335 sq. ft.Built-up : 1,335 sq. ft.
4 RM 103,840,000 Built-up : 118,000 sq. ft.Built-up : 118,000 sq. ft.

我想将它们放入 Pandas Dataframe 的另一个原因是我需要稍后在 Excel 中进行一些计算。

最佳答案

我相信你需要:

a = [data.text for data in price]
b = [data.text for data in BUA]

df = pd.DataFrame({'price':a, 'BUA':b}, columns=['price','BUA'])

关于python - 将网络抓取结果加载到 Pandas DataFrame 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46585132/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com