我已经构建了一个代码,可以从我网站的博客中提取信息(这些 URL 位于 Excel 文件中,因此我从那里提取这些信息)。我希望将我提取的每个 URL 信息放在单独的 .txt 文件中(到目前为止,我只设法将这些信息放在 1 中)。我怎样才能做到这一点?我不知道从哪里开始,我在这里很迷失:(任何帮助将不胜感激。import urllibfrom bs4 import BeautifulSoupimport pandas as pdimport timei = []crawl = pd.read_excel('C:/Users/Acer/Desktop/internal_all2.xlsx') addresses = crawl['Address'].tolist() for row in addresses: url = row time.sleep(5) response = urllib.request.urlopen(url) soup = BeautifulSoup(response, 'html.parser') content = soup.find_all('p') for content2 in content: print(url, content2) i.append([url,content2]) df = pd.DataFrame(i) df.to_csv('C:/Users/Acer/Desktop/scripts/content/test.txt', index=False)
1 回答
翻翻过去那场雪
TA贡献2065条经验 获得超13个赞
只需在文件名后附加一个字符串:
import urllib
from bs4 import BeautifulSoup
import pandas as pd
import time
i = []
crawl = pd.read_excel('C:/Users/Acer/Desktop/internal_all2.xlsx')
addresses = crawl['Address'].tolist()
for row in addresses:
url = row
time.sleep(5)
response = urllib.request.urlopen(url)
soup = BeautifulSoup(response, 'html.parser')
content = soup.find_all('p')
for content2 in content:
print(url, content2)
i.append([url,content2])
df = pd.DataFrame(i)
df.to_csv(f'C:/Users/Acer/Desktop/scripts/content/test_{url}.txt', index=False)
添加回答
举报
0/150
提交
取消