【发布时间】:2020-05-13 11:27:22
【问题描述】:
您好,我已经使用以下代码对数据表进行了网页抓取:
import requests
from bs4 import BeautifulSoup
import pandas as pd
import numpy as np
df = pd.DataFrame()
for row in links2get:
url = row
response = requests.get(url)
html_page = response.content
soup = BeautifulSoup(html_page, 'html.parser')
text = soup.find_all(text=True)
for a in soup.select('.trackM'):
b = a.get_text()
array = np.array(b)
print(array)
#reshape = ????
#df = df.append(reshape)
我拥有的数组的输出是:
print(array):
Table Title
Heading 1
Heading 2
Heading 3
Heading 4
Heading 5
1084
316
No
72
Yes
编辑有时表格中缺少值,因此可能存在奇数个元素(例如,5 个标题列但只有 4 个值)。
我希望重塑成一个 DataFrame,所以它看起来像:
print(df):
Heading 1 Heading 2 Heading 3 Heading 4 Heading 5
1084 316 No 72 Yes
我在重塑时遇到了麻烦,所以如果有人有任何建议,那就太好了!谢谢!
【问题讨论】:
标签: python pandas beautifulsoup