Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - Parsing a specific value from a URL within a line

I've managed to get my script to print out a line of text based on what is in the line:

if "cvename" in line:       
    CVE = list_of_line[4:5]     
    print "The CVE number is ", ' '.join(CVE)       
    print " "
    CVEfile.write("CVE-" + str(CVE) + '\n')

which prints out:

['http://cve.mitre.org/cgi-bin/cvename.cgi?name=1999-0016][Xref']

But what I want is just the value '1999-0016'

I'm assuming a regular expression can be used to do this but I don't have much experience in using them. What I've noticed is that the value I want to extract always starts with a year as it is a CVE number

like image 639
user2099445 Avatar asked Jan 23 '26 15:01

user2099445


1 Answers

Always try to use a more specific approach before using regular expressions. You need to parse an url? Use urlparse.

import urlparse

u = 'http://cve.mitre.org/cgi-bin/cvename.cgi?name=1999-0016'

q = urlparse.urlparse(u).query
values = urlparse.parse_qs(q).get('name')
if values is not None:
    print values[0]
    # prints '1999-0016'
like image 89
Pavel Anossov Avatar answered Jan 25 '26 06:01

Pavel Anossov