简体   繁体   中英

How can I read a list of urls from a text file and save all the responses into one text file?

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

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

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

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