[英]Extract certain values from a for loop
I have a for loop which prints values between 3 and 42.5, however, I want to assign a variable to only a certain range of values in this case between 1 and 8.我有一个 for 循环,它打印 3 到 42.5 之间的值,但是,在这种情况下,我只想将一个变量分配给 1 到 8 之间的特定范围的值。
with requests.Session() as s:
r = s.get(url, headers=headers)
soup = BeautifulSoup(r.text, 'html.parser')
sizes = soup.findAll(True,{'class':'product__sizes-size-1'})
for allsize in sizes:
print(allsize.text)
Where this outputs 3 4 4.5 5 5.5 6 6.5 7 8 36 37.5 38 38.5 39 40 40.5 41 42.5
其中输出
3 4 4.5 5 5.5 6 6.5 7 8 36 37.5 38 38.5 39 40 40.5 41 42.5
How would I assign a variable to only the values which are between 1 and 8我如何将变量分配给仅介于 1 和 8 之间的值
This should do:这应该做:
with requests.Session() as s:
r = s.get(url, headers=headers)
soup = BeautifulSoup(r.text, 'html.parser')
sizes = [item for item in soup.findAll(True,{'class':'product__sizes-size-1'}) if float(item.text) >=1 and float(item.text) <=8]
for allsize in sizes:
print(allsize.text)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.