Python urllib2';的结果与web浏览器的结果不同

Python urllib2';的结果与web浏览器的结果不同,python,request,response,urllib2,Python,Request,Response,Urllib2,我正在aws lambda中编写一个函数。功能简单。它只收到特定网站的请求 python2中的Lambda函数如下所示导入urllib2 def lambda_handler(event, context): # TODO implement url = "https://www.amazon.co.jp/s/field-keywords=4548967337259" response = urllib2.urlopen(url) #print response

我正在aws lambda中编写一个函数。功能简单。它只收到特定网站的请求

python2中的Lambda函数如下所示<还包括代码>导入urllib2

def lambda_handler(event, context):
    # TODO implement
    url = "https://www.amazon.co.jp/s/field-keywords=4548967337259"
    response = urllib2.urlopen(url)
    #print response

    return response.read() 
我将返回的值带到我的
rubyonrails
服务器,并试图解析必要的信息

在网站上,标签和相关信息如下所示

    <a class="a-link-normal a-text-normal" target="_blank" 
rel="noopener" href="https://www.amazon.co.jp/GOTHAM-
%E3%82%B5%E3%83%BC%E3%83%89-%E3%82%B7%E3%83%BC%E3%82%BA%E3%83%B3-
%E3%83%96%E3%83%AB%E3%83%BC%E3%83%AC%E3%82%A4-
%E3%82%B3%E3%83%B3%E3%83%97%E3%83%AA%E3%83%BC%E3%83%88-
%E3%83%9C%E3%83%83%E3%82%AF%E3%82%B9-Blu-ray/dp/B071K5VZTL/ref=sr_1_1?
ie=UTF8&amp;qid=1505293516&amp;sr=8-1&amp;keywords=4548967337259"> 
为什么会发生这种情况?我如何避免这种情况


实际上,我尝试了类似于
response.json()
的方法,但它无法完全生成as
json
表单

尝试传递
用户代理
标题:

import urllib2

def lambda_handler(...):
    request = urllib2.Request("http://www.google.com",
                           headers={"User-Agent" : "Mozilla/5.0"})
    return urllib2.urlopen(request).read()

您需要将响应传递给字符串函数:

 def lambda_handler(event, context):
    url = "https://www.amazon.co.jp/s/field-keywords=4548967337259"
    response = urllib2.urlopen(url)
    return str(response.read()) #here, casting as a string

response.read()
和将其转换为字符串有什么大区别?你能解释一下有标题和没有标题的区别吗?@jh.shin你可以“愚弄”服务器,让它认为你是一个网络浏览器。如果没有它,它似乎会为您提供不同的内容。
 def lambda_handler(event, context):
    url = "https://www.amazon.co.jp/s/field-keywords=4548967337259"
    response = urllib2.urlopen(url)
    return str(response.read()) #here, casting as a string