pylibcurl之https搜索引擎之网络数据抓取小例子,302moved?google搜索引擎不让你抓搜索结果??ok,此文问题通通解决

1 篇文章 0 订阅
1 篇文章 0 订阅

前提:操作平台-WIN7


一.首先你得python,我安装的是python2.7.9


二.其次,你得安装pyLibCurl,安装方式:http://pycurl.sourceforge.net/


三.最后,你得编写测试用例test.py:(当然,从代码中可以看出你电脑得有E盘,否则改一下代码,然后我抓取的数据是google一下test的数据)

#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vi:ts=4:et

import sys
import pycurl

class Test:
    def __init__(self):
        self.contents = ''

    def body_callback(self, buf):
        self.contents = self.contents + buf
        f = open('e:\\tmp\\tmp.html', 'w')
        print f
        f.write(self.contents)

sys.stderr.write("Testing %s\n" % pycurl.version)

t = Test()
c = pycurl.Curl()
c.setopt(c.URL, 'https://www.google.com/search?q=testx')

c.setopt(c.USERAGENT, "Mozilla/4.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0) in my heart of heart.")
c.setopt(c.HEADER, True)
c.setopt(c.REFERER, "https://www.google.com/search?q=testx")
c.setopt(c.COOKIEFILE, "./COOKIE.txt")
c.setopt(c.COOKIEJAR, "./COOKIE.txt")

c.setopt(c.WRITEFUNCTION, t.body_callback)
c.perform()
c.close()

print(t.contents)



延伸:

是不是你正常发起http请求google会告诉你“302 Moved”,ok,仔细研究一下这段代码,也会解决你的问题


参考:

http://superuser.com/questions/482470/google-302-moved-in-firefox

http://stackoverflow.com/questions/22570970/php-search-by-image-google-curl-return-302-moved



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

北京橙溪科技有限公司enwing.com

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值