0

I am trying to pull url from inside of html but seems that regex is not working. Any issue spotted ? Though when i take only a part of html for my website it works fine(have commented out that part of code)

I do know about scapy and beautifulSoap etc. but for due to restriction i don't want to use such modules.

    page="ANY-XYZ-WEBSITE"

    def extract_first_link():
        urlopener=urllib.urlopen(page)
        html=str(urlopener.read())
        matchObj = re.match( '<a href="(.*)/([0-9a-zA-Z-]+)"', html, re.I)
        #k = open ("file.txt",'w')
        #k.write(html)
        #print "matchObj.group() : ", matchObj.group(1)
        #matchObj = re.match( '<a href="(.*)/([0-9a-zA-Z-]+)"', html[4111:4150], re.M|re.I)
        print "matchObj.group() : ", matchObj.group()
        print "matchObj.group() : ", matchObj.group(1)
        print "matchObj.group() : ", matchObj.group(2)

    if __name__=="__main__":
        print extract_first_link()
3
  • 2
    I think you need to use re.search instead of re.match. match will only look at the beginning of the string. Commented Sep 2, 2015 at 12:22
  • 1
    try replacing (.*) with ([^/]*) Commented Sep 2, 2015 at 12:23
  • OOPs , thanks for pointing out. Commented Sep 2, 2015 at 12:23

1 Answer 1

1

re.match checks only the beginning of the string, re.search searches all the string.

Described here: https://docs.python.org/2/library/re.html

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.