Python Forum
Having issue with regular expressions
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Having issue with regular expressions
#1
For this homework assignment I have been tasked with web scraping using regex (I know it isn't best practice). I believe the code I have is correct, and the issue is to do with the regular expression itself, or how it has been implemented in the code

def web_scrape ():
    html = urlopen("https://www.amazon.com.au/gp/bestsellers/videogames/")
    html_contents = html.read().decode()
    htmlProducts = re.compile('<span\s(?:class=.p13n-sc-truncated.)>(.*)<\/span\>')
    htmlPrices = re.compile('<span\s(?:class=.p13n-sc-price.)>(.*)<\/span\>')
    products = re.findall(htmlProducts, html_contents)
    prices = re.findall(htmlPrices, html_contents)
    print(prices)
    print (products)
I used the regular expression in a provided regex tester (without the '' and () around them) and it worked, but for some reason am having issues actually using it in my program.

Any help would be wonderful. Thank you.

And just to add on, it's not returning any errors, just when I print, it only returns [].
Reply


Messages In This Thread
Having issue with regular expressions - by azulu - May-19-2020, 04:42 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Regular Expressions in Files (find all phone numbers and credit card numbers) Amirsalar 2 4,127 Dec-05-2017, 09:48 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

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