Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Google应用程序引擎-无法正确接收GZIP HTML文件_Python_Google App Engine_Gzip_Urllib2_Urlfetch - Fatal编程技术网

Python Google应用程序引擎-无法正确接收GZIP HTML文件

Python Google应用程序引擎-无法正确接收GZIP HTML文件,python,google-app-engine,gzip,urllib2,urlfetch,Python,Google App Engine,Gzip,Urllib2,Urlfetch,Python和Google应用程序引擎专家 我想检索位于以下链接的TD银行按揭利率网站: "http://tdbank.mortgagewebcenter.com/Default.asp" 今天晚上,我通过教程学习了Python和Google应用程序引擎,我认为这可能是GZIP的问题 理想情况下,我希望有人修复我粘贴在下面的代码。或者提供正确的代码(如果这更容易的话),以便成功接收此网页并能够在python/google应用程序引擎中解析它 尝试1-URLFETCH import webapp2

Python和Google应用程序引擎专家

我想检索位于以下链接的TD银行按揭利率网站:

"http://tdbank.mortgagewebcenter.com/Default.asp"

今天晚上,我通过教程学习了Python和Google应用程序引擎,我认为这可能是GZIP的问题

理想情况下,我希望有人修复我粘贴在下面的代码。或者提供正确的代码(如果这更容易的话),以便成功接收此网页并能够在python/google应用程序引擎中解析它

尝试1-URLFETCH

import webapp2
import gzip

import StringIO

from google.appengine.api import users
from google.appengine.api import urlfetch
from BeautifulSoup import BeautifulSoup

class MainPage(webapp2.RequestHandler):
    def get(self):
        url = "http://tdbank.mortgagewebcenter.com/Default.asp"
        result = urlfetch.fetch(url=url,headers={'User-Agent': 'Mozilla/5.0',
                                                 'Accept': 'text/html',
                                                 'Accept-Language': 'en-us,en',
                                                 'Accept-Encoding': 'gzip',
                                                 'Connection': 'keep-alive'})
        f = StringIO.StringIO(result.content)
        c = gzip.GzipFile(fileobj=f)
        content = c.read()
        self.response.out.write(content)

app = webapp2.WSGIApplication([('/', MainPage)],
                              debug=True)
尝试2-URLLIB2

import cgi
import webapp2
import gzip
import StringIO
import urllib2
import httplib

from BeautifulSoup import BeautifulSoup

class MainPage(webapp2.RequestHandler):
    def get(self):
        httplib.HTTPConnection.debuglevel = 1
        url = urllib2.Request('http://tdbank.mortgagewebcenter.com/Default.asp')
        url.add_header('Accept-encoding', 'gzip')
        url.add_header('User-Agent', 'Mozilla/5.0')
        opener = urllib2.build_opener()
        f = opener.open(url)
        compresseddata = f.read()
    compressedstream = StringIO.StringIO(compresseddata)
        c = gzip.GzipFile(fileobj=compressedstream)
        content = c.read()
        self.response.out.write(content)

app = webapp2.WSGIApplication([('/', MainPage)],
                              debug=True)
YAML文件:

application: fimrates
version: 2
runtime: python27
api_version: 1
threadsafe: true

handlers:
- url: /.*
script: fimrates.app
在这两种情况下,我的浏览器都会重定向到

http://localhost:8080/Default.asp?bhjs=1&bhqs=1
如果我将试图读取的URL更改为另一个网页,如www.google.com,输出将正确打印

提前谢谢你的帮助,我真的很感激


-Todd

您发布的Url在javascript中进行重定向。获得最终页面的唯一方法是模拟浏览器,这在GAE上是不可能的

我通过
curl-L下载了htmlhttp://tdbank.mortgagewebcenter.com/Default.asp
它给了我“不支持的浏览器”。这意味着此页面将在javascript中检查浏览器的类型


在fetch命令中,尝试添加参数“follow_redirects=True”。

如果使用curl下载页面时使用的是“不受支持的浏览器”,那么检查显然是在服务器端,而不是cient端。确切地说,它在两侧都使用逻辑-如果使用普通浏览器的用户代理,它将提供一个进行客户端检查的JS页面。试试看,如果gzip是传输编码,就不需要自己解压,urlfetch会帮你解压的。通常,查看数据并查看它是否与调试的第一步所期望的相似是一个好主意。