Python使用Httplib访问Web站点

本文介绍了一个简单的Python脚本,该脚本使用httplib2和libxml2库从Google获取昆明的天气信息。脚本通过HTTP请求获取XML格式的天气数据,并使用libxml2解析并打印这些数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    最简单的用法:

import httplib2
import libxml2

http = httplib2.Http()
response,content=http.request('http://www.google.com/ig/api?hl=en_us&weather=kunming','GET')
doc = libxml2.parseDoc(content)
print doc
doc.freeDoc()


    输出如下:

<?xml version="1.0"?>
<xml_api_reply version="1">
	<weather module_id="0" tab_id="0" mobile_row="0" mobile_zipped="1" row="0" section="0">
		<forecast_information>
			<city data="Kunming, Yunnan"/>
			<postal_code data="kunming"/>
			<latitude_e6 data=""/>
			<longitude_e6 data=""/>
			<forecast_date data="2012-05-08"/>
			<current_date_time data="2012-05-09 03:00:00 +0000"/>
			<unit_system data="US"/>
		</forecast_information>
		<current_conditions>
			<condition data="Clear"/>
			<temp_f data="64"/>
			<temp_c data="18"/>
			<humidity data="Humidity: 83%"/>
			<icon data="/ig/images/weather/sunny.gif"/>
			<wind_condition data="Wind: S at 7 mph"/>
		</current_conditions>
		<forecast_conditions>
			<day_of_week data="Tue"/>
			<low data="57"/>
			<high data="81"/>
			<icon data="/ig/images/weather/chance_of_storm.gif"/>
			<condition data="Chance of Storm"/>
		</forecast_conditions>
		<forecast_conditions>
			<day_of_week data="Wed"/>
			<low data="59"/>
			<high data="84"/>
			<icon data="/ig/images/weather/mostly_sunny.gif"/>
			<condition data="Mostly Sunny"/>
		</forecast_conditions>
		<forecast_conditions>
			<day_of_week data="Thu"/>
			<low data="59"/>
			<high data="81"/>
			<icon data="/ig/images/weather/thunderstorm.gif"/>
			<condition data="Thunderstorm"/>
		</forecast_conditions>
		<forecast_conditions>
			<day_of_week data="Fri"/>
			<low data="59"/>
			<high data="84"/>
			<icon data="/ig/images/weather/mostly_sunny.gif"/>
			<condition data="Partly Sunny"/>
		</forecast_conditions>
	</weather>
</xml_api_reply>


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值