You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

21 lines
649 B

from urllib.request import urlopen, Request
import re
r = urlopen(Request('https://books.toscrape.com', headers={'User-Agent': 'Mozilla/5.0'}))
html = r.read().decode('utf-8')
price_search = re.search(r'class="price_color[^"]*"[^>]*>([^<]+)<', html)
if price_search:
print('Found price pattern 1:', price_search.group(1))
else:
print('Pattern 1 not found')
price_search2 = re.search(r'price_color">([^<]+)<', html)
if price_search2:
print('Found price pattern 2:', price_search2.group(1))
else:
print('Pattern 2 not found')
idx = html.find('price_color')
if idx > 0:
print('Context around price_color:', html[idx-20:idx+50])