Python Forum
user validation for opening urls
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
user validation for opening urls
#1
HI All,

I' mlearning python and writing few programs to automate my testing. I am trying to do url validation based on what user has entered.If user inputs the version as 21.1.0 then I should be able to search for 21.1.10 in the base_url and if match found then only open the link. I'm struggling to find the match with the version user enters.
If I enter 1.1.0 it scans the website for 1.1.0 and if there's 21.1.0, it finds the match and prints vresion found. I want if it matches only with 21.1.0 then only open the link or if user enters 2.2.0 and there's 2.2.0 then I should find the match for 2.2.0 and not for 12.2.0.
Please note this is not the correct base_url.

import urllib2
import re
base_url = "http://artifactory.int.abc.com:9090/artifactory/simple/test-local/com/abc/test/test.far/"
user_input = raw_input("Enter the test version you want to upgrade to: ")

#connect to a URL
website = urllib2.urlopen(base_url)

#read html code
html = website.read()
print(html)

test2 = re.search(user_input, html)
if user_input in html:
    print("version found")
else:
    print("version not found")
Reply
#2
What character is before the version sequence in the url? Can you give a similar url where these versions are in the url?
Recommended Tutorials:
Reply
#3
Hi,

Quote:I' m learning python
Based on your code, <you are using Python 2 - which is in less than 6 month EOL. Today, there is no reason when learning Python to use 2.7. Use a supported 3.x version instead.

Although it does not solve your original problem, it makes your code future proof :-)

On your problem: using re.match instead of re.search should do what you want (Explanation).
By the way: what does the page holding the versions look like? Probably you could do the whole sting by simple string comparison. Your current code may next to no use of the power of regular expressions.

Regards, noisefloor
Reply
#4
I'm getting the following output. As a user if I enter the user_input as 1.1.0, it should go find the match for "1.1.0" and not "11.1.0"
Basically I want web crawling without installing any additional modules.
Output:
1.1.0 1.1.2 1.2.1 1.2.2 1.3.1 11.1.0 12.1.2
Reply
#5
As metulburr already asked: what characters are before version? Can you give sample url? Are they always with same structure?
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#6
There's no charachter. These vresions number are folders. I want to do a user validation. When user enters, 1.1.0 and there's no folder 1.1.0 then it should throw error for usert to notify to enter correct version.
Example would be:

import urllib2
import re
base_url = "https://www.python.org/doc/versions/"
user_input = raw_input("Enter the test version you want to Download: ")
 
#connect to a URL
website = urllib2.urlopen(base_url)
 
#read html code
html = website.read()
print(html)
x= user_input.strip() 
test2 = re.match(x, html)
if user_input in html:
    print("version found")
else:
    print("version not found")
Reply
#7
If your matching the exact input then you dont need regex. Parse down the url to the version and compare to the input given.
>>> '1.1.0' in '21.1.0'
True
>>> '1.1.0' == '21.1.0'
False
Recommended Tutorials:
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Using Python to search through a list of urls jeremy 4 2,877 Dec-18-2019, 11:52 AM
Last Post: Malt
  Urls in a file to be executed pyseeker 2 2,042 Sep-09-2019, 03:38 PM
Last Post: pyseeker
  opening a file from a windows desktop folder- error opening file Charan007 1 2,988 Dec-06-2018, 11:50 AM
Last Post: buran
  Looping URLs breaks them PythonStudent 2 2,928 Apr-21-2018, 02:54 PM
Last Post: PythonStudent
  listening to user input even after opening an application in mac sharma16aug 4 4,946 Jan-24-2017, 10:43 AM
Last Post: sharma16aug

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020