请教,怎么爬天气网的 json 数据

2017-08-10 18:00:26 +08:00
 visitantzj

比如这个页面: http://m.weather.com.cn/maqi/101020100.shtml

json 格式数据类似: http://d1.weather.com.cn/aqi_7d/XiangJiAqiFc5d/101020100.html

整个 Request Headers(Referer, cookie 之类)完全设了还是返回 403,请教一下问题出在哪里

3992 次点击
所在节点    Python
16 条回复
zhihaofans
2017-08-10 18:02:00 +08:00
ua
mrl1996
2017-08-10 18:02:55 +08:00
要我说还是换个爬取
visitantzj
2017-08-10 18:05:43 +08:00
@zhihaofans
也设了,chrome 里正常访问的 request header 我都给弄了进去,还是不行

{'Host': 'd1.weather.com.cn',
'Connection': 'keep-alive',
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36',
'Accept': '*/*'',
'Referer': 'http://m.weather.com.cn/mweather/101020100.shtml',
'Accept-Encoding': 'gzip, deflate, sdch',
'Accept-Language': 'zh-CN,zh;q=0.8',
'Cookie': 'BIGipServerd1src_pool=1874396221.20480.0000'}
lxml
2017-08-10 18:13:39 +08:00
```
import requests

cookies = {
# 自己填写
}

headers = {
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'zh-CN,zh;q=0.8',
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36',
'Accept': '*/*',
'Referer': 'http://m.weather.com.cn/maqi/101020100.shtml',
'Connection': 'keep-alive',
}

params = (
('_', '14483821791309'),
)

a = requests.get('http://d1.weather.com.cn/aqi_all/101020100.html', headers=headers, params=params, cookies=cookies)
a.encoding = 'utf-8'
print(a.text)
```
刚刚用 curl 生成了一份代码,实测是可以的,你可能是没带 cookie ?
q409195961
2017-08-10 18:16:14 +08:00
visitantzj
2017-08-10 18:24:56 +08:00
@lxml 感谢老兄!

看来应该是 cookie 的问题,我这写法里 urllib.request.Request 应该是没把 cookie 传递出去。非常感谢!知道大方向自己就好去 google 了

url = r"http://d1.weather.com.cn/aqi_7d/XiangJiAqiFc5d/101020100.html"
headers = {
'Host': 'd1.weather.com.cn',
'Connection': 'keep-alive',
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36',
'Accept': '*/*',
'Referer': 'http://m.weather.com.cn/maqi/101020100.shtml',
'Accept-Encoding': 'gzip, deflate, sdch',
'Accept-Language': 'zh-CN,zh;q=0.8',
'Cookie': 'f_city=%E9%95%87%E6%B1%9F%7C101190301%7C; BIGipServerd1src_pool=1874396221.20480.0000'
}
req = urllib.request.Request(url= url, headers=headers)

resp = urllib.request.urlopen(url)
respBytes = resp.read()
html = respBytes.decode('utf-8')
visitantzj
2017-08-10 18:26:38 +08:00
@q409195961 不好意思,这个是开始试另一个页面时候的 url 没改过来,我感觉应该是像楼上说的是 cookie 问题……自己先研究一下先
q409195961
2017-08-10 18:33:22 +08:00
RorschachZZZ
2017-08-10 18:40:06 +08:00
不用爬吧,现在好多免费 api 接口
visitantzj
2017-08-10 18:48:21 +08:00
@q409195961

正无地自容中……其实问题很无厘头 resp = urllib.request.urlopen(**url**)里 url 忘记改成 req 了

@RorschachZZZ

感觉天气网的数据比较正规点,其实原先也有 api 的,估计后来用的人多就关了
yefuchao
2017-08-10 19:20:08 +08:00
t123yh
2017-08-10 20:02:45 +08:00
用彩云天气的免费接口呀
Bijiabo
2017-08-10 20:04:22 +08:00
楼主的需求是为了天气数据吧?可以找免费的 API 接口,感觉彩云天气的就不错
jingniao
2017-08-10 20:39:23 +08:00
这叫我想起来大学一门课程结课作业做的一个简单的安卓天气了,当时做的很烂很烂……
xd314697475
2017-08-10 20:58:31 +08:00
需要天气接口的话,直接去
http://openweathermap.org/current
提供多种 api,免费使用
wencan
2017-08-11 16:41:53 +08:00
以前我用的中国天气网开放平台的接口 openweather.weather.com.cn 已经不可访问了
现在中国天气网整了个智慧气象服务 不知道是不是升级版 http://smart.weather.com.cn/

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/381997

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX