全球主机交流论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

IP归属甄别会员请立即修改密码
查看: 761|回复: 7
打印 上一主题 下一主题

求php大神帮忙解决个curl问题,可付费

[复制链接]
跳转到指定楼层
1#
发表于 2019-3-16 19:02:01 | 只看该作者 回帖奖励 |正序浏览 |阅读模式
本帖最后由 cnzmz 于 2019-3-16 20:11 编辑

补充一下:用python也能获取到源码,不出现验证码,python源码在最下面,就是php的curl死活不行

用php curl函数抓取谷歌学术搜索结果的时候,一直会出现google的人机验证,我原以为是谷歌限制了我的服务器ip,于是我用这个服务器搭建了一个http代理,自己的浏览器用这个代理访问google学术,无论怎么刷新怎么搜索,都不会出现验证码。那么问题应该不是出现在ip上面,然后我又在curl函数里加入了header信息,加入了UA,但还是出现验证码,不知道是不是我没加正确。

以下为我写的curl函数代码,我自己浏览器里面设置这个http代理访问没有任何问题,用下面的函数访问就给我蹦出了个人机验证

跪求php大神能帮忙解决,可付费

  1. <?php
  2. function get_html($url){
  3.         $header=array();
  4.         $header[] = "Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8";
  5.         $header[] = "Accept-Language:zh-CN,zh;q=0.9,en;q=0.8";
  6.         $header[] = "Cache-Control:no-cache";
  7.         $header[] = "Connection:keep-alive"; // browsers keep this blank.
  8.         $header[] = "DNT:1";
  9.         $header[] = "Pragma:no-cache";
  10.         $header[] = "Upgrade-Insecure-Requests:1";
  11.         $header[] = "User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36";
  12.         $proxy="ip:port";
  13.         $proxyauth = 'user:pass';
  14.         $ch = curl_init();
  15.         curl_setopt ($ch, CURLOPT_PROXY, $proxy);
  16.         curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxyauth);
  17.         curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  18.         curl_setopt($ch, CURLOPT_URL,$url);
  19.         curl_setopt($ch, CURLOPT_HEADER, false);
  20.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  21.         curl_setopt($ch, CURLOPT_TIMEOUT,5);
  22.         $data = curl_exec($ch);
  23.         curl_close($ch);
  24.         return $data;
  25. }
  26. $a= get_html('https://scholar.google.com/scholar?hl=zh-CN&as_sdt=0%2C5&q=cell&btnG=');
  27. echo '<pre>';
  28. print_r($a);
  29. echo '</pre>';
复制代码


补充一下,用python也能获取到源码,不会出现机器人验证

  1. def requests_html(url):
  2.         headers = {       
  3.         'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
  4.         'Accept-Encoding':'gzip, deflate, sdch',
  5.         'Accept-Language':'zh-CN,zh;q=0.8',
  6.         'Cache-Control':'no-cache',
  7.         'Connection':'keep-alive',
  8.         'DNT':'1',
  9.         'Pragma':'no-cache',
  10.         'Upgrade-Insecure-Requests':'1',
  11.         'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36',
  12.         }
  13.         # proxies = {'http':'127.0.0.1:1080','https':'127.0.0.1:1080'}
  14.         proxies = {"http": "http://user:pass@ip:port"}
  15.         r = requests.get(url,headers=headers,timeout=5,proxies=proxies)
  16.         status_code = r.status_code
  17.         # print status_code
  18.         if status_code != 200:
  19.                 pass
  20.         else:
  21.                 coding = r.encoding.strip().lower()
  22.                 # print coding
  23.                 if coding == 'utf-8':       
  24.                         html = r.content
  25.                         # print html
  26.                         return html                       
  27.                 else:
  28.                         html = r.text#.encode(coding).decode('utf8').encode('utf8')
  29.                         # print html
  30.                         return html
复制代码
8#
 楼主| 发表于 2019-3-16 21:05:50 | 只看该作者

用这个办法,还是会出现机器人验证,大佬可否加个QQ请教一下?
7#
 楼主| 发表于 2019-3-16 20:29:36 | 只看该作者

感动的我都哭了,我去研究下,有结果了来反馈;请接下我的膝盖
6#
发表于 2019-3-16 20:24:01 | 只看该作者
提示: 作者被禁止或删除 内容自动屏蔽
5#
 楼主| 发表于 2019-3-16 20:12:28 | 只看该作者
补充一下,用python的request也能获取到源码,不出现验证码
4#
 楼主| 发表于 2019-3-16 19:55:15 | 只看该作者
rlonnet 发表于 2019-3-16 19:41
来路加上试试,不过google就算了 代理也不行

来路加上也不行,
3#
发表于 2019-3-16 19:41:48 | 只看该作者
来路加上试试,不过google就算了 代理也不行
2#
 楼主| 发表于 2019-3-16 19:29:20 | 只看该作者
自己来终结0回复的尴尬
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|全球主机交流论坛

GMT+8, 2026-1-14 07:19 , Processed in 0.060623 second(s), 9 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表