日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python字符串垂直输出加循环_将漂亮的soup嵌套循环垂直输出到datafram中

發布時間:2025/3/11 python 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python字符串垂直输出加循环_将漂亮的soup嵌套循环垂直输出到datafram中 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

我如何將這個web抓取(數據)的輸出放入pandas數據幀?

下面的代碼只顯示一行數據,并且是垂直排列的。如何使數據幀看起來完全像打印(數據)-即水平?

另外,我是個新手,這可不是Python。請建議如何使其成為函數(?)?

我嘗試了所有類型的append和concat函數,但是嵌套循環會使一切都陷入錯誤。請幫忙。

提前謝謝你。在from bs4 import BeautifulSoup as bs

import urllib.request

import re

import pandas as pd

Url1 = 'http://www5.statcan.gc.ca/cimt-cicm/topNCountryCommodities-marchandises?lang=eng&chapterId=27&sectionId=0&refMonth='

Url2 = '&refYr='

Url3 = '&freq=6&countryId=999&usaState=0&provId=1&arrayId=9900000&commodityId=271111&commodityName=Natural+gas%2C+liquefied&topNDefault=10&tradeType=3'

regexQ = re.compile('Date1 Qty')

regexC = re.compile('Footnote')

regexV = re.compile('Date1 Val')

import csv

import collections

for year in range(2016, 2018):

for month in range(0, 13):

sauce = urllib.request.urlopen(Url1+str(month)+Url2+str(year)+Url3).read().decode('utf-8')

soup = bs(sauce, 'lxml')

select = soup.find_all('option', selected=True)

for i in select:

Month=select[4]

Year=select[3]

month=Month.text

year=Year.text

#print(year)

#print(month, year)

value = [x.text.strip() for x in soup.find_all("td", {"headers": regexV})]

country = [x.text.strip() for x in soup.find_all("a", {"href": regexC})]

quantity = [x.text.strip() for x in soup.find_all("td", {"headers": regexQ})]

total_list = [list(x) for x in zip(country,quantity,value)]

for item in total_list:

h=[year , month]

data = h + item

print(data)

df = pd.DataFrame(data=data)

df.head

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的python字符串垂直输出加循环_将漂亮的soup嵌套循环垂直输出到datafram中的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。