我想找到img src ="([^"] +)"的每个实例。之前是div class =" grid"并由div class =" orderplacebut"继承在一些HTML代码中,即我想在div容器中找到名为" grid"的所有图像。
如果我使用findall它只会返回一个图像,因为div class =" grid"在网页上只出现一次,因此它只返回以下图像URL之一(有意义)。所以我想迭代findall正则表达式,使其再次运行,并返回图像URL的第二个实例,然后是第三个实例,依此类推。这可能是使用finditer,我将如何在代码中使用它?
下面的代码是我的findall正则表达式,只返回一个URL。
from urllib import urlopen
from re import findall
import re
dennisov_url = 'https://denissov.ru/en/'
dennisov_html = urlopen(dennisov_url).read()
# Print all images between div class="grid" and div class="orderplacebut"
# Because the regex spans over several lines, use DOTALL flag to include
# every character between, including new lines
watch_image_urls = findall('<div class="grid".*<img src="([^"]+)".*<div class="orderplacebut"', dennisov_html, flags=re.DOTALL)
print watch_image_urls
答案 0 :(得分:0)
真的,使用另一种解析器方法(由于此处被阻止的.ru
域未进行测试):
import requests
from bs4 import BeautifulSoup
dennisov_url = 'https://denissov.ru/en/'
dennisov_html = requests.get(dennisov_url)
soup = BeautifulSoup(dennisov_html.text, 'lxml')
images = soup.select('div.grid > img')