Python 3.x 如何在没有for循环的情况下抓取url列表?

Python 3.x 如何在没有for循环的情况下抓取url列表?,python-3.x,list,for-loop,beautifulsoup,Python 3.x,List,For Loop,Beautifulsoup,我有一批url列表,我想抓取这些url上的一些信息 daa = ['https://old.reddit.com/r/Games/comments/a2p1ew/', 'https://old.reddit.com/r/Games/comments/9zzo0e/', 'https://old.reddit.com/r/Games/comments/a31a6q/', ] for y in daa: uClient = requests.get(y, headers = {'User-agen

我有一批url列表,我想抓取这些url上的一些信息

daa = ['https://old.reddit.com/r/Games/comments/a2p1ew/', 'https://old.reddit.com/r/Games/comments/9zzo0e/', 'https://old.reddit.com/r/Games/comments/a31a6q/', ]

for y in daa:
uClient = requests.get(y, headers = {'User-agent': 'your bot 0.1'})
page_soup = soup(uClient.content, "html.parser")
time= page_soup.findAll("p", {"class":"tagline"})[0].time.get('datetime').replace('-', '')
而且我能很好地获得所有我想要的
时间。但是我需要在没有for循环的情况下完成它,或者我的意思是我需要在下一步中
打开
并编写一个文件,但是如果我在同一个循环中完成,输出会很奇怪。
在没有for循环的情况下,如何获得时间?

您可以如上所述使用
打开(文件'a')
。或者我喜欢做的是将所有内容追加到一个表中,然后将整个内容作为文件写入

import requests
import bs4 
import pandas as pd


results = pd.DataFrame()

daa = ['https://old.reddit.com/r/Games/comments/a2p1ew/', 'https://old.reddit.com/r/Games/comments/9zzo0e/', 'https://old.reddit.com/r/Games/comments/a31a6q/', ]

for y in daa:
    w=1
    uClient = requests.get(y, headers = {'User-agent': 'your bot 0.1'})
    page_soup = bs4.BeautifulSoup(uClient.content, "html.parser")
    time= page_soup.findAll("p", {"class":"tagline"})[0].time.get('datetime').replace('-', '')

    temp_df = pd.DataFrame([[y, time]], columns=['url','time'])
    results = results.append(temp_df).reset_index(drop = True)

result.to_csv('path/to_file.csv', index=False) 

打开(文件,'a')
(附加到文件末尾)是否不满足要求?您的意思是输出很奇怪。它可以帮助你展示你正在获得的产出,以及你试图实现的产出。