-2

I have a list of urls inside a text file. I would like to feed the urls from the text file into my script of code instead of pasting each url into the script but unsure how to do so. I also would like to save all the responses into one text file. Thank you

import requests
from datetime import datetime

def run():

    url = "https://en.wikipedia.org/wiki/NBA"

    payload = {}
    headers= {}

    response = requests.request("POST", url, headers=headers, data = payload)

    print(response.text)

startTime = datetime.now()
run()
print(datetime.now() - startTime)

File: Wiki.text

https://en.wikipedia.org/wiki/NBA
https://en.wikipedia.org/wiki/NFL
https://en.wikipedia.org/wiki/MLB
https://en.wikipedia.org/wiki/NHL

2 Answers2

1

you can do something like this:

import requests
list_of_pages = open('list_of_links.txt', 'r+')
save = open('output.txt', 'a+')
save.truncate(0)
for page_link in list_of_pages:
    url = page_link
    payload = {}
    headers = {}
    response = requests.request("POST", url, headers=headers, data=payload)
    save.write(str(response) + "\n")
hedy
  • 1,160
  • 5
  • 23
icecube
  • 111
  • 2
  • 16
1

Try this, it returns the code of each page in Wiki.txt and also the time it took to get it:

import requests
from datetime import datetime
def run():
    links_file = open('Wiki.txt', 'r')
    outputs_file = open('outputs.txt', 'a')
    for link in links_file.readlines():  # run for ever line in file
            payload = {}
            headers = {}

            response = requests.request("POST", link, headers=headers, data=payload)

            outputs_file.write(response.text + '\n')
    links_file.close()
    outputs_file.close()

startTime = datetime.now()
run()
print(datetime.now() - startTime)

Basically all you needed to do was add a loop that runs for every line in the file, and add the resulting code to another text file.

F.M
  • 1,369
  • 1
  • 16
  • 31