Jump to content

Open Txt file and webrequest

- - - - -

  • Please log in to reply
4 replies to this topic

#1
martin2311

martin2311

    Learning Programmer

  • Members
  • PipPipPip
  • 32 posts
guys i want to make a script like this


1.Open a text file containing urls
2.send a web request to all url one by one (post some data also in field of "name=" )
3.check the response of the sent page and if it fulfills condition open new text file and add that url to that new made file
4.loop the whole code for all urls in the txt file opened in 1


any help guys

#2
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
I have no idea what the condition is, but this is a relatively basic task. Have you learned much Python? Opening a file is just as easy as using the open() function, then using readlines() to get the whole content of the file in a list of lines. You're rather nebulous about what web request you mean, just getting the URL contents itself (IE getting the HTML)? Could you please be more specific as to what you're trying to do?

Something like this:
handle = open("urlfile.txt", "r")
outfile = open("output.txt", "w")
for line in handle.readlines():
    url_content = urllib.urlopen("{url}#name={name}".format(url = line[:-1], name = some_name))
    # used [:-1] in line to remove the ending \n from line for the url, else you'd have bad formatting.
    if (passed_condition(url_content)):
        outfile.write(line)
That code should get the contents of any URL and pass, as a URL argument, a name assigned to some_name. Again, I have no idea what passed_condition is, so I just left it as another function. Does this help, or is it something else you want?
Wow I changed my sig!

#3
martin2311

martin2311

    Learning Programmer

  • Members
  • PipPipPip
  • 32 posts
no m new to python my first code every i and i can`t understand where i have to make changes to adjust the condition like i want to match the text "Signout" where i can fix that part????

#4
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
This is your first attempt at programming in Python? Well, this is going to be a little more challenging then... what do you know about Python, are you familiar with control loops, creating objects, passing arguments to functions, etc.? If not, and you really need this program, I suggest you find someone who can write it for you for payment. If this is just a learning exercise, I think you should consider something a bit simpler until you understand the basic concepts of Python and can navigate multi-file programs.
Wow I changed my sig!

#5
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,722 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
A Fibonacci sequence generator is usually a good one to get started with, after the obligatory Hello World.
sudo rm -rf /




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users