简体   繁体   中英

Write multiple .txt files / Python loop

I'm scraping multiple URL's of a website with BeautifulSoup and want to generate a file for each 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()

I'm generating a unique file 'app.txt' how can I generate a file for each URL ? Thank you

Just replace this:

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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM