Home > Mobile >  Search for a word in webpage and save to TXT in Python
Search for a word in webpage and save to TXT in Python

Time:06-28

I am trying to: Load links from a .txt file, search for a specific Word, and if the word exists on that webpage, save the link to another .txt file but i am getting error: No scheme supplied. Perhaps you meant http://<_io.TextIOWrapper name='import.txt' mode='r' encoding='cp1250'>? Note: the links has HTTPS://

The code:

import requests
list_of_pages = open('import.txt', 'r ')
save = open('output.txt', 'a ')
word = "Word"
save.truncate(0)
for page_link in list_of_pages:
    res = requests.get(list_of_pages)
    if word in res.text:
     response = requests.request("POST", url)
    save.write(str(response)   "\n")

Can anyone explain why ? thank you in advance !

CodePudding user response:

Try putting http:// behind the links.

CodePudding user response:

When you use res = requests.get(list_of_pages) you're creating HTTP connection to list_of_pages. But requests.get takes URL string as a parameter (e.g. http://localhost:8080/static/image01.jpg), and look what list_of_pages is - it's an already opened file. Not a string. You have to either use requests library, or file IO API, not both.

If you have an already opened file, you don't need to create HTTP request at all. You don't need this request.get(). Parse list_of_pages like a normal, local file.

Or, if you would like to go the other way, don't open this text file in list_of_arguments, make it a string with URL of that file.

  • Related