【发布时间】:2021-09-02 18:32:56
【问题描述】:
您好,我有以下循环,它产生一个字典作为输出:
for l in soup.find_all('a'):
link = (l.get('href'))
if link.count('2021') == 1:
stuff = urllib.parse.urlsplit(link)
stuff = stuff.path.split('/')
list(stuff)
if stuff[1] == '2021':
stuff = np.array(stuff)
year = stuff[1]
month = stuff[2]
day = stuff[3]
category = stuff[4]
keywords = stuff[5:7]
dict = {'year': year, 'month': month, 'day': day, 'category': category, 'keywords': keywords}
输出如下:
/Users/Programowanie/PycharmProjects/pythonProject1/venv/bin/python /Users/Programowanie/PycharmProjects/pythonProject1/main.py
year month day category keywords
0 2021 09 02 us supreme-court-texas-abortion-law.html
year month day category keywords
0 2021 09 02 us supreme-court-texas-abortion-law.html
year month day category keywords
0 2021 09 02 us supreme-court-texas-abortion-law.html
year month day category keywords
0 2021 09 02 us politics
1 2021 09 02 us biden-abortion-texas-law.html
year month day category keywords
0 2021 09 02 us politics
1 2021 09 02 us biden-abortion-texas-law.html
我想将所有单独的字典排序到一个单独的 DataFrame 中,以便将年、月、日、类别、关键字的值保存在相应的年、月、日、类别、关键字列中。你知道怎么做吗?
提前致谢
【问题讨论】:
-
pd.concat(output)假设output是您的字典列表。
标签: python pandas web-scraping