首页 > 解决方案 > Python从img打印URL +进入新页面

问题描述

我想在链接末尾的变量中输入下一页链接。

我是 Python 新手 :(

这是我的脚本:

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')

可以在以下位置找到脚本输出: https ://1.amazonaws.com/awer/adc/45521192_15642345066.jpg

我可以让它只打印 45521192 所以输出会是这样的:

45521192
45521193
45521194
45521195

标签: pythonpython-3.xbeautifulsoupurllib

解决方案


当然可以,只需更改print(image['src']+'\n')为:

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

推荐阅读