Python网页抓取

Python网页抓取,python,web-scraping,beautifulsoup,Python,Web Scraping,Beautifulsoup,我想在属性与标记中的特定值匹配时返回字符串值。在运行下面的代码时,我没有得到任何值。事实上,我希望分别基于“class”属性值“myforecast current”、“myforecast current lrg”和“myforecast current sm”获取所有字符串(即阴霾、39°F、4°C)。Pl提供正确代码的帮助 if soup.p['class'] == 'myforecast-current-lrg': print(soup.p.string) 阴霾 39°

我想在属性与标记中的特定值匹配时返回字符串值。在运行下面的代码时,我没有得到任何值。事实上,我希望分别基于“class”属性值“myforecast current”、“myforecast current lrg”和“myforecast current sm”获取所有字符串(即阴霾、39°F、4°C)。Pl提供正确代码的帮助

if soup.p['class'] == 'myforecast-current-lrg':
   print(soup.p.string)

阴霾

39°;F

4°;C


您必须了解
BeautifulSoup
如何查找元素

我知道这个方法,我想应该还有其他的方法

查找所有数据('p',class='myforecast-current-lrg'):
打印(data.string)


您的代码返回阴霾39°F 4°C。您期望的输出是什么?第一个有效,谢谢…如果每次单击都获取不同城市的温度,是否有一种方法可以从单个html文件中提取所有城市的温度,或者我需要获取每个城市的html文件并通过beautifulsoup传递?希望你得到我的问题一个html文件包含了城市的温度,然后你可以得到所有城市的温度在一次。
for data in soup.find_all('p'):
    if 'myforecast-current-lrg' in data['class']:
        print(data.string)