Search code examples
pythonpython-3.xbeautifulsoupurllib

Python prints URL from img + going to new page


I want to enter the next page link in a variable at the end of the link.

I'm new in Python :(

Here is my script:

from urllib.request import urlopen
from bs4 import BeautifulSoup
import re

html = urlopen('https://test.com/index/index/2')
bs = BeautifulSoup(html, 'html.parser')
images = bs.find_all('img', {'src':re.compile('.jpg')})
for image in images: 
    print(image['src']+'\n')

The script output can be found at: https://1.amazonaws.com/awer/adc/45521192_15642345066.jpg

Can I make it print only 45521192 so the output will be like this:

45521192
45521193
45521194
45521195

Solution

  • Sure you can, just change print(image['src']+'\n') to:

    print(image['src'].split('/')[-1].split('_')[0])