Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/352.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
Python 试图在类中查找数字_Python_Beautifulsoup_Python Requests - Fatal编程技术网

Python 试图在类中查找数字

Python 试图在类中查找数字,python,beautifulsoup,python-requests,Python,Beautifulsoup,Python Requests,因此,我正在查看的类没有属性名称。这两种我都试过了。当我搜索=containers=soup.find_all('div',class='today'u nowcard-temp')时,它返回一行我想要的值。但我无法提取数字。因为我想要的行没有类属性名,所以我无法直接从find函数中获取编号。如果我做了containers=soup.find('span',class=None,它不会返回我想要的值。这就是我目前所拥有的全部内容: import requests from bs4 import

因此,我正在查看的类没有属性名称。这两种我都试过了。当我搜索=
containers=soup.find_all('div',class='today'u nowcard-temp')
时,它返回一行我想要的值。但我无法提取数字。因为我想要的行没有类属性名,所以我无法直接从find函数中获取编号。如果我做了
containers=soup.find('span',class=None
,它不会返回我想要的值。这就是我目前所拥有的全部内容:

import requests
from bs4 import BeautifulSoup

url = 'https://weather.com/en-CA/weather/today/l/8663b88e4a1c7d6068b7f33e360396ac1c89f3dde9533082cd342aef06ad1e87'

page = requests.get(url)

soup = BeautifulSoup(page.text, 'html.parser')
containers = soup.find_all('div', class_='today_nowcard-temp')


print(containers)
使用
find
()而不是
find_all
(),然后执行
find_next('span')

如果要使用find_all(),请按如下所示进行迭代

containers = soup.find_all('div', class_='today_nowcard-temp')
for item in containers:
   print(item.find_next('span').text)
也可以使用css选择器

print(soup.select_one("div.today_nowcard-temp > span").text)
使用
find
()而不是
find_all
(),然后执行
find_next('span')

如果要使用find_all(),请按如下所示进行迭代

containers = soup.find_all('div', class_='today_nowcard-temp')
for item in containers:
   print(item.find_next('span').text)
也可以使用css选择器

print(soup.select_one("div.today_nowcard-temp > span").text)

非常感谢!这成功了!还有一件事,学位符号被打印成问号。有解决办法吗?我在控制台上得到了正确的值没有问号。非常感谢!这成功了!还有一件事,学位符号被打印成问号。有解决办法吗?我得到了正确的值控制台没有问号。您不能简单地使用正则表达式从字符串中获取数字吗?另外,您可以添加预期的输出吗?您不能简单地使用正则表达式从字符串中获取数字吗?另外,您可以添加预期的输出吗?