Notepad++Good Luck To You!

php爬取国家商标局公告数据
<?php
/**
 * 发送post请求
 * @param string $url 请求地址
 * @param array $param post键值对数据或者urlencode数据
 * @return string
 */

function send_post($url = '', $param = '')
{
$param = is_array($param) ? http_build_query($param) : $param;
if (empty($url) || empty($param)) {
return false;
}
$postUrl = $url;
$curlPost = $param;
$ch = curl_init();//初始化curl
curl_setopt($ch, CURLOPT_URL, $postUrl);//抓取指定网页
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type:application/x-www-form-urlencoded"));
curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
curl_setopt($ch, CURLOPT_TIMEOUT, 60*15);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
$data = curl_exec($ch);//运行curl
curl_close($ch);
return $data;
}


$t1 = microtime(true);

$listurl="http://sbgg.saic.gov.cn:9080/tmann/annInfoView/annSearchDG.html";
//page=页数,rows每次查询行数,annNum=期数
$listdata='page=2&rows=20&annNum=1593&annType=&tmType=&coowner=&recUserName=&allowUserName=&byAllowUserName=&appId=&appIdZhiquan=&bfchangedAgengedName=&changeLastName=&transferUserName=&acceptUserName=&regName=&tmName=&intCls=&fileType=&totalYOrN=false&appDateBegin=&appDateEnd=';

$selbycodeurl="http://sbgg.saic.gov.cn:9080/tmann/annInfoView/selectInfoidBycode.html";
//annNum=期数
$selbycodedata="annNum=1593&annTypecode=TMZCSQ";

$imgcode=send_post($selbycodeurl, $selbycodedata);
$imgviewurl="http://sbgg.saic.gov.cn:9080/tmann/annInfoView/imageView.html";

$result=json_decode(send_post($listurl, $listdata), true);

foreach ($result['rows'] as $key => $value) {
$imgviewdata="id={$imgcode}&pageNum={$value['page_no']}&flag=1";
//id是根据期数查询接口返回的数据,pageNum是单个商标数据中的page_no字段,组合查询所在图片列表
$imgdata=json_decode(send_post($imgviewurl, $imgviewdata), true);
$result['rows'][$key]['pic']=$imgdata['imaglist'][3];  //图像来自于接口列表第4张图
var_dump($result['rows'][$key]);
}
$t2 = microtime(true);
echo '耗时'.round($t2-$t1, 3).'秒';


运行结果如下,受接口限制,因为每个商标数据需要单独请求图片接口获取数据,平均每20个商标数据,需要80秒,用服务器估计会快一点。


image.png

«    2023年7月    »
12
3456789
10111213141516
17181920212223
24252627282930
31
TOP 搜索
TOP 控制面板
您好,欢迎到访网站!
  查看权限
TOP 最新留言
    TOP 作者列表
    TOP 站点信息
    • 文章总数:163
    • 页面总数:0
    • 分类总数:6
    • 标签总数:20
    • 评论总数:0
    • 浏览总数:312004
    召唤伊斯特瓦尔