Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 带迭代器的正则表达式_Regex_Python 3.x - Fatal编程技术网

Regex 带迭代器的正则表达式

Regex 带迭代器的正则表达式,regex,python-3.x,Regex,Python 3.x,我正试图从网上商店中削价。我正在遍历页面上的产品,并将其包含在正则表达式中。尽管避开了花括号,正则表达式仍然不起作用。(findall返回一个空列表) soup.findall返回的HTML代码: [<div class="ps4-price at-min-price-1"> from 29 GBP </div>] [<div class="ps4-price at-min-price-2"> from 35 GBP </div>] [来自29

我正试图从网上商店中削价。我正在遍历页面上的产品,并将其包含在正则表达式中。尽管避开了花括号,正则表达式仍然不起作用。(findall返回一个空列表)

soup.findall返回的HTML代码:

[<div class="ps4-price at-min-price-1"> from 29 GBP </div>]
[<div class="ps4-price at-min-price-2"> from 35 GBP </div>]
[来自29 GBP]
[从35英镑起]
Python coode:

for product in range(21):

        min_prices_text = str(soup.findAll("div", class_="ps4-price at- 
        min- price-{}".format(product)))

        min_price = re.findall('<div class="ps4-price at-min-price- 
        {{}}"> (.+?)<'.format(product), str(min_prices_text))
适用于范围(21)内的产品:
最低价格文本=str(soup.findAll(“div”,class=“ps4价格在-
最小-价格-{}.格式(产品)))

min_price=re.findall(“(.+?)您可以访问使用
findall
获得的元素的
.string
属性,并且只将正则表达式应用于纯文本。例如,由于您在那里只需要一个整数,因此您可以对这些字符串应用
re.sub(r'\D+,'',minu prices\u text.string)

请参见示例代码:

results = []
for product in range(21):
    min_prices_text = soup.find("div", class_="ps4-price at-min-price-{}".format(product))
    if min_prices_text:
        results.append(re.sub(r'\D+', '', min_prices_text.string))

print(results) # => ['29', '35']

如果要将字符串列表转换为整数,请使用
list(map(int,results))

尝试
min\u prices=soup.find_all(“div”,class=“ps4 price”)
然后
arr=[]
用于el in min\u prices:
arr.sub(r'\D+','',el.string))
=>打印(list(map(int,arr)))
。如果您需要确保列出了两个类,请尝试
min\u prices=soup.find\u all(“div”,class\u=re.compile(r“ps4 price at min price-\d+”)
可能不使用正则表达式解析HTML内容。顺便说一句,您的格式化字符串已损坏,
{}
实际上是两个大括号。您需要使用单个大括号,
{}
,在那里。