2 回答
TA贡献1884条经验 获得超4个赞
您可以直接将值列表作为行添加到数据框中。我设置了具体的列,并且列表被添加到与这些列匹配的数据帧中。
browser.get('https://fortune.com/global500/2019/walmart')
data =[]
df = pd.DataFrame(columns = ['c1', 'c2', 'c3', 'c4', 'c5','c6','c7'])
i = 1
while True:
table = browser.find_element_by_css_selector('tbody')
if i > 2:
break
try:
print("Scraping Page no. " + str(i))
i = i + 1
values =[]
for row in table.find_elements_by_css_selector('tr'):
value = ([cell.text for cell in row.find_elements_by_css_selector('td.dataTable__value--3n5tL.dataTable__valueAlignLeft--3uvNx')])
values.append(value)
print(values)
s = pd.Series(values,index=df.columns)
df = df.append(s,ignore_index=True)
try:
WebDriverWait(browser, 5).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a > span.singlePagination__icon--2KbZn"))).click()
time.sleep(3)
except TimeoutException:
break
except Exception as e:
print(e)
break
print(df)
browser.quit()
输出:
c1 c2 ... c6 c7
0 [C. Douglas McMillon] [Retailing] ... [25] [2,200,000]
1 [Dai Houliang] [Energy] ... [21] [619,151]
TA贡献1829条经验 获得超13个赞
你只需要使用熊猫转置函数:
df_transposed = data1.T
输出:
0 C. Douglas McMillon Retailing General Merchandisers Bentonville, Ark. - ...
1 Dai Houliang Energy Petroleum Refining Beijing
添加回答
举报