在python中使用beautifulsoup遇到麻烦

我是python的新手,对下面的代码有麻烦。我正在尝试获取温度或网站上的日期,但似乎无法获得输出。我尝试了许多变体,但似乎仍然无法正确完成。

谢谢您的帮助!

#Code below: 
import requests,bs4
r = requests.get('http://www.hko.gov.hk/contente.htm')
print r.raise_for_status()
hkweather = bs4.BeautifulSoup(r.text)
print hkweather.select('div left_content fnd_day fnd_date')
帕德拉克·坎宁安(Padraic Cunningham)

您的css选择器不正确,您应该.在标记和css类之间使用,想要的标记位于div中,且fnd_day该类位于div内,且ID为fnd_content

divs = soup.select("#fnd_content div.fnd_day")

但这仍然无法获取数据,因为它是通过ajax请求动态生成的,您可以使用以下代码json格式获取所有数据

u = "http://www.hko.gov.hk/wxinfo/json/one_json.xml?_=1468955579991"

data = requests.get(u).json()

from pprint import pprint as pp
pp(data)

这几乎返回了所有动态内容,包括日期和时间等。

如果访问键F9D,则可以看到有关所有临时性和日期的一般天气描述:

from pprint import pprint as pp

pp(data['F9D'])

输出:

{'BulletinDate': '20160720',
 'BulletinTime': '0315',
 'GeneralSituation': 'A southwesterly airstream will bring showers to the '
                     'coast of Guangdong today. Under the dominance of an '
                     'upper-air anticyclone, it will be generally fine and '
                     'very hot over southern China in the latter part of this '
                     'week and early next week.',
 'NPTemp': '25',
 'WeatherForecast': [{'ForecastDate': '20160720',
                      'ForecastIcon': 'pic53.png',
                      'ForecastMaxrh': '95',
                      'ForecastMaxtemp': '32',
                      'ForecastMinrh': '70',
                      'ForecastMintemp': '26',
                      'ForecastWeather': 'Sunny periods and a few showers. '
                                         'Isolated squally thunderstorms at '
                                         'first.',
                      'ForecastWind': 'South to southwest force 4.',
                      'IconDesc': 'Sunny Periods with A Few Showers',
                      'WeekDay': '3'},
                     {'ForecastDate': '20160721',
                      'ForecastIcon': 'pic90.png',
                      'ForecastMaxrh': '90',
                      'ForecastMaxtemp': '33',
                      'ForecastMinrh': '65',
                      'ForecastMintemp': '28',
                      'ForecastWeather': 'Mainly fine and very hot apart from '
                                         'isolated showers in the morning.',
                      'ForecastWind': 'South to southwest force 3 to 4.',
                      'IconDesc': 'Hot',
                      'WeekDay': '4'},
                     {'ForecastDate': '20160722',
                      'ForecastIcon': 'pic90.png',
                      'ForecastMaxrh': '90',
                      'ForecastMaxtemp': '33',
                      'ForecastMinrh': '65',
                      'ForecastMintemp': '28',
                      'ForecastWeather': 'Mainly fine and very hot apart from '
                                         'isolated showers in the morning.',
                      'ForecastWind': 'Southwest force 3.',
                      'IconDesc': 'Hot',
                      'WeekDay': '5'},
                     {'ForecastDate': '20160723',
                      'ForecastIcon': 'pic90.png',
                      'ForecastMaxrh': '90',
                      'ForecastMaxtemp': '34',
                      'ForecastMinrh': '65',
                      'ForecastMintemp': '29',
                      'ForecastWeather': 'Fine and very hot.',
                      'ForecastWind': 'Southwest force 3.',
                      'IconDesc': 'Hot',
                      'WeekDay': '6'},
                     {'ForecastDate': '20160724',
                      'ForecastIcon': 'pic90.png',
                      'ForecastMaxrh': '90',
                      'ForecastMaxtemp': '34',
                      'ForecastMinrh': '65',
                      'ForecastMintemp': '29',
                      'ForecastWeather': 'Fine and very hot.',
                      'ForecastWind': 'Southwest force 3.',
                      'IconDesc': 'Hot',
                      'WeekDay': '0'},
                     {'ForecastDate': '20160725',
                      'ForecastIcon': 'pic90.png',
                      'ForecastMaxrh': '90',
                      'ForecastMaxtemp': '33',
                      'ForecastMinrh': '65',
                      'ForecastMintemp': '29',
                      'ForecastWeather': 'Mainly fine and very hot apart from '
                                         'isolated showers in the morning.',
                      'ForecastWind': 'South to southwest force 3.',
                      'IconDesc': 'Hot',
                      'WeekDay': '1'},
                     {'ForecastDate': '20160726',
                      'ForecastIcon': 'pic90.png',
                      'ForecastMaxrh': '90',
                      'ForecastMaxtemp': '33',
                      'ForecastMinrh': '65',
                      'ForecastMintemp': '29',
                      'ForecastWeather': 'Mainly fine and very hot apart from '
                                         'isolated showers in the morning.',
                      'ForecastWind': 'South to southwest force 3.',
                      'IconDesc': 'Hot',
                      'WeekDay': '2'},
                     {'ForecastDate': '20160727',
                      'ForecastIcon': 'pic90.png',
                      'ForecastMaxrh': '90',
                      'ForecastMaxtemp': '33',
                      'ForecastMinrh': '65',
                      'ForecastMintemp': '28',
                      'ForecastWeather': 'Mainly fine and very hot apart from '
                                         'isolated showers in the morning.',
                      'ForecastWind': 'Southwest force 3 to 4.',
                      'IconDesc': 'Hot',
                      'WeekDay': '3'},
                     {'ForecastDate': '20160728',
                      'ForecastIcon': 'pic90.png',
                      'ForecastMaxrh': '90',
                      'ForecastMaxtemp': '33',
                      'ForecastMinrh': '65',
                      'ForecastMintemp': '28',
                      'ForecastWeather': 'Mainly fine and very hot apart from '
                                         'isolated showers in the morning.',
                      'ForecastWind': 'Southwest force 3 to 4.',
                      'IconDesc': 'Hot',
                      'WeekDay': '4'}]}

唯一的查询字符串参数是可以使用时间lib生成纪元时间戳

from time import time
u = "http://www.hko.gov.hk/wxinfo/json/one_json.xml?_={}".format(int(time()))

data = requests.get(u).json()

不传递时间戳也会返回相同的数据,因此我将留给您调查其重要性。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在Python中使用str(count)遇到麻烦

来自分类Dev

在Swift中使用多节TableView遇到麻烦

来自分类Dev

在xtensor中使用xt :: where遇到麻烦

来自分类Dev

我在Excel中使用宏遇到麻烦

来自分类Dev

使用readline()函数Python遇到麻烦

来自分类Dev

尝试在python中使用gdata和oauth2遇到麻烦

来自分类Dev

在python中使用Selenium进行Web抓取,单击按钮时遇到麻烦

来自分类Dev

Python和BeautifulSoup-使用<dt>和<dd>标签在div中指定字段时遇到麻烦

来自分类Dev

在if语句中使用布尔方法遇到麻烦

来自分类Dev

在Django中使用自定义http标题遇到麻烦

来自分类Dev

在Rails中使用Foundation和Turbolinks遇到麻烦

来自分类Dev

在奇异果中使用pyo遇到麻烦

来自分类Dev

在R中使用Tidyverse分离数据时遇到麻烦

来自分类Dev

在Java中使用递归方法时遇到麻烦

来自分类Dev

在Laravel 4中使用Multi-Auth遇到麻烦

来自分类Dev

在奇异果中使用pyo遇到麻烦

来自分类Dev

在Vaadin中使用BeanItemContainer和TreeTable遇到麻烦

来自分类Dev

在ApplescriptObjc Xcode项目中使用Applescript遇到麻烦

来自分类Dev

在C#中使用委托时遇到麻烦

来自分类Dev

在where.not在rails中使用时遇到麻烦

来自分类Dev

在Spark中使用数据时遇到麻烦了吗?

来自分类Dev

在nodejs中使用grok模块时遇到麻烦

来自分类Dev

使用Github遇到麻烦

来自分类Dev

使用XPATH遇到麻烦

来自分类Dev

使用pygame在python屏幕上移动对象时遇到麻烦

来自分类Dev

我在使用python制作列表列表时遇到麻烦

来自分类Dev

使用python Sympy计算泰勒级数时遇到麻烦

来自分类Dev

使用指针时遇到麻烦

来自分类Dev

使用Java的FileChooser遇到麻烦

Related 相关文章

热门标签

归档