-6

我正在遍历 a 中的所有img'request.POST以查看它们是否是 HTTPS(我正在使用Beautiful Soup来提供帮助)

这是我的代码:

content = request.POST['content']
print(content) #prints:
<p>test test test</p><br><p><img src="https://www.treefrogfarm.com/store/images/source/IFE_A-K/ClarySage2.jpg" alt=""></p><br><p>2nd 2nd</p><br><p><img src="https://www.treefrogfarm.com/store/images/source/IFE_A-K/ClarySage2.jpg" alt=""></p>

soup = BeautifulSoup(content, 'html.parser')
for image in soup.find_all('img'):
    print('Source:', image.get('src')[:8]) #prints Source: https://
    if image.get('src')[:7] == "https://":
        print('HTTPS')
    else:
        print('Not HTTPS')

即使image.get('src')[:7] == "https://",代码仍然打印Not HTTPS

知道为什么吗?

4

2 回答 2

3

对于初学者来说,'https://'是 8 个字符,所以 7 个字符的切片无法匹配它。

另外,请让您的问题标题实际表明您遇到的问题,而不是对 python 操作员的无关指控。

于 2018-07-13T23:50:33.707 回答
0

为了匹配https://字符串,适当的切片将:8代替:7

if image.get('src')[:8] == "https://":

于 2018-07-13T23:53:14.080 回答