- 积分
- 287
- 鸿鹄币
- 个
- 好评度
- 点
- 精华
- 最后登录
- 1970-1-1
- 阅读权限
- 30
- 听众
- 收听
初级工程师
  
|
又是一年台风来袭季,第6号台风“烟花”还没有抵达,第7号台风“查帕卡”已将出发。都说现代生活节奏太快,现代台风也要赶场子吗?对于要遭受台风地区的小伙伴,既想要享受它带来的清凉,又不想目睹它裹挟的灾难。
关于一些台风的数据,我们是可以通过Python获取到的。首先我们找到关于台风的网址信息就可以。然后挂上代理,获取数据就可以了。完整展示如下:
<?php
// 要访问的目标页面
$url = "http://typhoon.zjwater.gov.cn";
$urls = "http://typhoon.zjwater.gov.cn";
// 代理服务器(产品官网 www.16yun.cn)
define("PROXY_SERVER", "tcp://t.16yun.cn:31111");
// 代理身份信息
define("PROXY_USER", "16GHVCJA");
define("PROXY_PASS", "077607");
$proxyAuth = base64_encode(PROXY_USER . ":" . PROXY_PASS);
// 设置 Proxy tunnel
$tunnel = rand(1,10000);
$headers = implode("\r\n", [
"Proxy-Authorization: Basic {$proxyAuth}",
"Proxy-Tunnel: ${tunnel}",
]);
$sniServer = parse_url($urls, PHP_URL_HOST);
$options = [
"http" => [
"proxy" => PROXY_SERVER,
"header" => $headers,
"method" => "GET",
'request_fulluri' => true,
],
'ssl' => array(
'SNI_enabled' => true, // Disable SNI for https over http proxies
'SNI_server_name' => $sniServer
)
];
print($url);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
var_dump($result);
// 访问 HTTPS 页面
print($urls);
$context = stream_context_create($options);
$result = file_get_contents($urls, false, $context);
var_dump($result);
?>
curl
GuzzleHttp
虽然现在台风还没有正式侵入。不过假设你以后需要这些资料的时候,获取就很是方便了,最基础的requests就可以了。就算不懂爬虫的也可以做得到爬取。 |
|