Table des matières
回复讨论(解决方案)
Maison développement back-end tutoriel php 关于抓取网站数据,出现302重定向!

关于抓取网站数据,出现302重定向!

Jun 23, 2016 pm 02:00 PM

目标网站:http://www.dltm.net/webtmq/free/free_query.php
第一步:输入商标号,提交(post抓起)

第二步:点击商标号

第三步:需要抓取的数据


前两步均已实现抓取,但抓取最后一步时老是出现302重定向,导致数据抓取不到。
相关代码:

//第一步define(TARGET_URL,'http://www.dltm.net/webtmq/free/free_query.php');define(REFFER_URL,'http://www.dltm.net');$url=TARGET_URL;$ch=curl_init($url);curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//返回结果存放在变量中,而不是默认的直接输出curl_setopt($ch, CURLOPT_HEADER, 0);curl_setopt($ch, CURLOPT_REFERER, REFFER_URL);$result= curl_exec($ch);//保存输出的页面到$result中curl_close($ch);preg_match_all('<input type="hidden" name="ip" value="(.*)">',$result,$rs);//第二步$fields_post = array(	'ip'=>$rs[1][0],	'textarea_explain'=>'%B2%E9%D1%AF%C8%AB%B2%BF%C0%E0%B1%F0',	'tm_lb'=> '0',	'tm_key'=>'8437927',	'tm_key_item'=>'tm_zch',	'query_mode'=>'1');$fields_string='';foreach($fields_post as $key => $value){    $fields_string .= $key . '=' . $value . '&';}$fields_string = rtrim($fields_string,'&');define(TARGET_URL1,'http://www.dltm.net/webtmq/free/free_res.php');define(REFFER_URL1,'http://www.dltm.net/webtmq/free/free_res.php');$url=TARGET_URL1;$ch=curl_init($url);curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//返回结果存放在变量中,而不是默认的直接输出curl_setopt($ch, CURLOPT_HEADER, 0);curl_setopt($ch, CURLOPT_REFERER, REFFER_URL1);curl_setopt($ch,CURLOPT_POST,1);//以POST方式提交curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);$result= curl_exec($ch);//保存输出的页面到$result中curl_close($ch);//第三步preg_match_all('<a href="(.*)" target="detail">',$result,$res);$url = 'http://www.dltm.net/webtmq/free/'.$res[1][0];$ch=curl_init($url);curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//返回结果存放在变量中,而不是默认的直接输出curl_setopt($ch, CURLOPT_HEADER, 0);curl_setopt($ch, CURLOPT_REFERER, REFFER_URL1);$result= curl_exec($ch);//保存输出的页面到$result中curl_close($ch);print_r($result);exit;//这一步得不到数据
Copier après la connexion

可以自己copy一下代码运行一下,第三步数据得不到,大神能帮我看看么,如果能得到数据,请贴出你的源码,非常感谢!!!


回复讨论(解决方案)

增加
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");

增加
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");

不行啊,打印curl_getinfo($ch)

Array ( [url] => http://www.dltm.net/webtmq/free/free_detail.php?ip=7f6179b25de2974b0e0b33d01c2fef18&class_id=25&id=10309774 [content_type] => text/html [http_code] => 302 [header_size] => 440 [request_size] => 193 [filetime] => -1 [ssl_verify_result] => 0 [redirect_count] => 0 [total_time] => 0.094 [namelookup_time] => 0 [connect_time] => 0.047 [pretransfer_time] => 0.047 [size_upload] => 0 [size_download] => 0 [speed_download] => 0 [speed_upload] => 0 [download_content_length] => -1 [upload_content_length] => 0 [starttransfer_time] => 0.094 [redirect_time] => 0 [certinfo] => Array ( ) [redirect_url] => http://www.dltm.net/webtmq/free/free_query.php )
Copier après la connexion

怎么不可以呢?
以注册号 8437927 为例
新建一文件 cookie.txt,执行代码

$cookie = realpath('cookie.txt'); //这是增加的//第一步define('TARGET_URL','http://www.dltm.net/webtmq/free/free_query.php');define('REFFER_URL','http://www.dltm.net');$url=TARGET_URL;$ch=curl_init($url);curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//返回结果存放在变量中,而不是默认的直接输出curl_setopt($ch, CURLOPT_HEADER, 0);curl_setopt($ch, CURLOPT_REFERER, REFFER_URL);curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie); //这是增加的curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie); //这是增加的$result= curl_exec($ch);//保存输出的页面到$result中curl_close($ch);preg_match_all('<input type="hidden" name="ip" value="(.*)">',$result,$rs); //第二步$fields_post = array(    'ip'=>$rs[1][0],    'textarea_explain'=>'%B2%E9%D1%AF%C8%AB%B2%BF%C0%E0%B1%F0',    'tm_lb'=> '0',    'tm_key'=>'8437927',    'tm_key_item'=>'tm_zch',    'query_mode'=>'1'); $fields_string='';foreach($fields_post as $key => $value){    $fields_string .= $key . '=' . $value . '&';}$fields_string = rtrim($fields_string,'&'); define('TARGET_URL1','http://www.dltm.net/webtmq/free/free_res.php');define('REFFER_URL1','http://www.dltm.net/webtmq/free/free_res.php');$url=TARGET_URL1;$ch=curl_init($url);curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//返回结果存放在变量中,而不是默认的直接输出curl_setopt($ch, CURLOPT_HEADER, 0);curl_setopt($ch, CURLOPT_REFERER, REFFER_URL1);curl_setopt($ch,CURLOPT_POST,1);//以POST方式提交curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie); //这是增加的curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie); //这是增加的$result= curl_exec($ch);//保存输出的页面到$result中curl_close($ch); //第三步preg_match_all('<a href="(.*)" target="detail">',$result,$res);$url = 'http://www.dltm.net/webtmq/free/'.$res[1][0]; $ch=curl_init($url);curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//返回结果存放在变量中,而不是默认的直接输出curl_setopt($ch, CURLOPT_HEADER, 0);curl_setopt($ch, CURLOPT_REFERER, REFFER_URL1);curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie); //这是增加的curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie); //这是增加的$result= curl_exec($ch);//保存输出的页面到$result中curl_close($ch);print_r($result);exit;
Copier après la connexion
Copier après la connexion

怎么不可以呢?
以注册号 8437927 为例
新建一文件 cookie.txt,执行代码

$cookie = realpath('cookie.txt'); //这是增加的//第一步define('TARGET_URL','http://www.dltm.net/webtmq/free/free_query.php');define('REFFER_URL','http://www.dltm.net');$url=TARGET_URL;$ch=curl_init($url);curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//返回结果存放在变量中,而不是默认的直接输出curl_setopt($ch, CURLOPT_HEADER, 0);curl_setopt($ch, CURLOPT_REFERER, REFFER_URL);curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie); //这是增加的curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie); //这是增加的$result= curl_exec($ch);//保存输出的页面到$result中curl_close($ch);preg_match_all('<input type="hidden" name="ip" value="(.*)">',$result,$rs); //第二步$fields_post = array(    'ip'=>$rs[1][0],    'textarea_explain'=>'%B2%E9%D1%AF%C8%AB%B2%BF%C0%E0%B1%F0',    'tm_lb'=> '0',    'tm_key'=>'8437927',    'tm_key_item'=>'tm_zch',    'query_mode'=>'1'); $fields_string='';foreach($fields_post as $key => $value){    $fields_string .= $key . '=' . $value . '&';}$fields_string = rtrim($fields_string,'&'); define('TARGET_URL1','http://www.dltm.net/webtmq/free/free_res.php');define('REFFER_URL1','http://www.dltm.net/webtmq/free/free_res.php');$url=TARGET_URL1;$ch=curl_init($url);curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//返回结果存放在变量中,而不是默认的直接输出curl_setopt($ch, CURLOPT_HEADER, 0);curl_setopt($ch, CURLOPT_REFERER, REFFER_URL1);curl_setopt($ch,CURLOPT_POST,1);//以POST方式提交curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie); //这是增加的curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie); //这是增加的$result= curl_exec($ch);//保存输出的页面到$result中curl_close($ch); //第三步preg_match_all('<a href="(.*)" target="detail">',$result,$res);$url = 'http://www.dltm.net/webtmq/free/'.$res[1][0]; $ch=curl_init($url);curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//返回结果存放在变量中,而不是默认的直接输出curl_setopt($ch, CURLOPT_HEADER, 0);curl_setopt($ch, CURLOPT_REFERER, REFFER_URL1);curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie); //这是增加的curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie); //这是增加的$result= curl_exec($ch);//保存输出的页面到$result中curl_close($ch);print_r($result);exit;
Copier après la connexion
Copier après la connexion


谢谢,刚接触curl,cookie位置少放了
问题已解决,结贴!!!!

Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn

Outils d'IA chauds

Undresser.AI Undress

Undresser.AI Undress

Application basée sur l'IA pour créer des photos de nu réalistes

AI Clothes Remover

AI Clothes Remover

Outil d'IA en ligne pour supprimer les vêtements des photos.

Undress AI Tool

Undress AI Tool

Images de déshabillage gratuites

Clothoff.io

Clothoff.io

Dissolvant de vêtements AI

AI Hentai Generator

AI Hentai Generator

Générez AI Hentai gratuitement.

Article chaud

R.E.P.O. Crystals d'énergie expliqués et ce qu'ils font (cristal jaune)
2 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌
Repo: Comment relancer ses coéquipiers
1 Il y a quelques mois By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: Comment obtenir des graines géantes
4 Il y a quelques semaines By 尊渡假赌尊渡假赌尊渡假赌
Combien de temps faut-il pour battre Split Fiction?
3 Il y a quelques semaines By DDD

Outils chauds

Bloc-notes++7.3.1

Bloc-notes++7.3.1

Éditeur de code facile à utiliser et gratuit

SublimeText3 version chinoise

SublimeText3 version chinoise

Version chinoise, très simple à utiliser

Envoyer Studio 13.0.1

Envoyer Studio 13.0.1

Puissant environnement de développement intégré PHP

Dreamweaver CS6

Dreamweaver CS6

Outils de développement Web visuel

SublimeText3 version Mac

SublimeText3 version Mac

Logiciel d'édition de code au niveau de Dieu (SublimeText3)

11 meilleurs scripts de raccourcissement d'URL PHP (gratuit et premium) 11 meilleurs scripts de raccourcissement d'URL PHP (gratuit et premium) Mar 03, 2025 am 10:49 AM

Les longues URL, souvent encombrées de mots clés et de paramètres de suivi, peuvent dissuader les visiteurs. Un script de raccourcissement d'URL offre une solution, créant des liens concis idéaux pour les médias sociaux et d'autres plateformes. Ces scripts sont utiles pour les sites Web individuels

Travailler avec les données de session Flash dans Laravel Travailler avec les données de session Flash dans Laravel Mar 12, 2025 pm 05:08 PM

Laravel simplifie la gestion des données de session temporaires à l'aide de ses méthodes de flash intuitives. Ceci est parfait pour afficher de brefs messages, alertes ou notifications dans votre application. Les données ne persistent que pour la demande ultérieure par défaut: $ demande-

Construisez une application React avec un Laravel Back End: Partie 2, React Construisez une application React avec un Laravel Back End: Partie 2, React Mar 04, 2025 am 09:33 AM

Il s'agit de la deuxième et dernière partie de la série sur la construction d'une application React avec un back-end Laravel. Dans la première partie de la série, nous avons créé une API RESTful utilisant Laravel pour une application de liste de base sur le produit. Dans ce tutoriel, nous serons Dev

Misque de réponse HTTP simplifié dans les tests Laravel Misque de réponse HTTP simplifié dans les tests Laravel Mar 12, 2025 pm 05:09 PM

Laravel fournit une syntaxe de simulation de réponse HTTP concise, simplifiant les tests d'interaction HTTP. Cette approche réduit considérablement la redondance du code tout en rendant votre simulation de test plus intuitive. L'implémentation de base fournit une variété de raccourcis de type de réponse: Utiliser illuminate \ support \ faades \ http; Http :: faux ([[ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

Curl dans PHP: Comment utiliser l'extension PHP Curl dans les API REST Curl dans PHP: Comment utiliser l'extension PHP Curl dans les API REST Mar 14, 2025 am 11:42 AM

L'extension PHP Client URL (CURL) est un outil puissant pour les développeurs, permettant une interaction transparente avec des serveurs distants et des API REST. En tirant parti de Libcurl, une bibliothèque de transfert de fichiers multi-protocol très respectée, PHP Curl facilite Efficient Execu

12 meilleurs scripts de chat PHP sur Codecanyon 12 meilleurs scripts de chat PHP sur Codecanyon Mar 13, 2025 pm 12:08 PM

Voulez-vous fournir des solutions instantanées en temps réel aux problèmes les plus pressants de vos clients? Le chat en direct vous permet d'avoir des conversations en temps réel avec les clients et de résoudre leurs problèmes instantanément. Il vous permet de fournir un service plus rapide à votre personnalité

Annonce de l'enquête sur la situation en 2025 PHP Annonce de l'enquête sur la situation en 2025 PHP Mar 03, 2025 pm 04:20 PM

L'enquête sur le paysage PHP 2025 étudie les tendances actuelles de développement du PHP. Il explore l'utilisation du cadre, les méthodes de déploiement et les défis, visant à fournir des informations aux développeurs et aux entreprises. L'enquête prévoit la croissance de la PHP moderne versio

Notifications à Laravel Notifications à Laravel Mar 04, 2025 am 09:22 AM

Dans cet article, nous allons explorer le système de notification dans le framework Web Laravel. Le système de notification de Laravel vous permet d'envoyer des notifications aux utilisateurs sur différents canaux. Aujourd'hui, nous discuterons de la façon dont vous pouvez envoyer des notifications OV

See all articles