我正在使用BeautifulSoup抓取网站的多个URL,并希望为每个URL生成一个文件。
categories = ["NEWS_AND_MAGAZINES", "ART_AND_DESIGN",...,"FAMILY"]
subcategories = ["topselling_free",...,"topgrossing"]
urls = []
for i in range (0,len(categories)):
for j in range (0,len(subcategories)):
url = categories_url_prefix + categories[i]+'/collection/'+subcategories[j]
urls.extend([url])
for i in urls:
response = get(i)
html_soup = BeautifulSoup(response.text, 'html.parser')
app_container = html_soup.find_all('div', class_="card no-rationale square-cover apps small")
file = open("apps.txt","a+")
for i in range(0, len(app_container)):
print(app_container[i].div['data-docid'])
file.write(app_container[i].div['data-docid'] + "\n")
file.close()
我正在生成一个唯一的文件“ app.txt”,如何为每个URL生成一个文件?谢谢
答案 0 :(得分:2)
只需替换一下:
for n, i in enumerate(urls):
response = get(i)
html_soup = BeautifulSoup(response.text, 'html.parser')
app_container = html_soup.find_all('div', class_="card no-rationale square-cover apps small")
with open("file{}.txt".format(n),"a+") as f:
for i in range(0, len(app_container)):
print(app_container[i].div['data-docid'])
f.write(app_container[i].div['data-docid'] + "\n")