Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/334.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
Python3使用urllib.request访问同一url失败,但请求成功?_Python_Python 3.x_Python Requests_Urllib - Fatal编程技术网

Python3使用urllib.request访问同一url失败,但请求成功?

Python3使用urllib.request访问同一url失败,但请求成功?,python,python-3.x,python-requests,urllib,Python,Python 3.x,Python Requests,Urllib,我感到困惑。 我的想法是,我想将请求发送到url,然后提取网页中的POST数据,然后发送到网页。当我在python中使用urllib.request时,我失败了,但是我使用了请求,它工作了! 请告诉我为什么。。。。 这是代码,注释是我使用urllib.request的代码 import urllib.request import http.cookiejar import re import requests loginUrl='https://passport.csdn.net/accou

我感到困惑。 我的想法是,我想将请求发送到url,然后提取网页中的POST数据,然后发送到网页。当我在python中使用urllib.request时,我失败了,但是我使用了请求,它工作了! 请告诉我为什么。。。。 这是代码,注释是我使用urllib.request的代码

import urllib.request
import http.cookiejar
import re
import requests


loginUrl='https://passport.csdn.net/account/login?from=http://my.csdn.net/my/mycsdn'

#Here is the urllib.request code
#cookies=http.cookiejar.MozillaCookieJar()
#handler=urllib.request.HTTPCookieProcessor(cookies)
#opener=urllib.request.build_opener(handler)
headers={
        'Origin': 'http://passport.csdn.net',
        'Referer':'http://passport.csdn.net/account/login?from=http%3A%2F%2Fmy.csdn.net%2Fmy%2Fmycsdn',
        'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.99 Safari/537.36 LBBROWSER'
        }

#Here is the requests code
s = requests.Session()
data = s.get(loginUrl)
data = data.text
#request = urllib.request.Request(loginUrl)
#response = urllib.request.urlopen(request)
#data = response.read().decode('utf-8')


#l get the value of lt and execution from the web page
pattern_lt = re.compile('<input type="hidden" name="lt" value="(.*?)" />',re.S)
lt = re.findall(pattern_lt,data)
lt = lt[0]
pattern_exe = re.compile('<input type="hidden" name="execution" value="(.*?)" />',re.S)
exe = re.findall(pattern_exe,data)
exe = exe[0]
postDict = {
        'username':'qinyufeng_hdq@163.com',
        'password':'csdn690076598',
        'lt':lt,
        'execution':exe,
        '_eventId':'submit'
        }
r = s.post(loginUrl, data=postDict)
#postData = urllib.parse.urlencode(postDict).encode()
#request = urllib.request.Request(loginUrl, postData,headers)
#response = opener.open(request)
#data = response.read().decode('UTF-8')
print (r.text)
导入urllib.request
导入http.cookiejar
进口稀土
导入请求
罗吉努尔酒店https://passport.csdn.net/account/login?from=http://my.csdn.net/my/mycsdn'
#以下是urllib.request代码
#cookies=http.cookiejar.MozillaCookieJar()
#handler=urllib.request.HTTPCookieProcessor(cookies)
#opener=urllib.request.build\u opener(处理程序)
标题={
“来源”:http://passport.csdn.net',
“Referer”:http://passport.csdn.net/account/login?from=http%3A%2F%2Fmy.csdn.net%2Fmy%2Fmycsdn',
“用户代理”:“Mozilla/5.0(Windows NT 6.1;WOW64)AppleWebKit/537.36(KHTML,如Gecko)Chrome/39.0.2171.99 Safari/537.36浏览器”
}
#这是请求代码
s=请求。会话()
data=s.get(loginUrl)
data=data.text
#request=urllib.request.request(loginUrl)
#response=urllib.request.urlopen(请求)
#data=response.read().decode('utf-8')
#从网页上获取lt和执行的值
模式\u lt=re.compile(“”,re.S)
lt=关于findall(模式,数据)
lt=lt[0]
模式_exe=re.compile(“”,re.S)
exe=re.findall(模式exe,数据)
exe=exe[0]
后记={
“用户名”:“秦玉峰”_hdq@163.com',
“密码”:“csdn690076598”,
“lt”:lt,
“执行”:exe,
“\u eventId”:“提交”
}
r=s.post(loginUrl,data=postDict)
#postData=urllib.parse.urlencode(postDict.encode())
#request=urllib.request.request(loginUrl、postData、headers)
#响应=opener.open(请求)
#data=response.read().decode('UTF-8')
打印(右文本)

我的英语不好,希望你能理解我的想法,谢谢你阅读我的问题。

你有没有试着和fiddler一起检查这两个请求到底是什么样的?我猜它们并不完全相同(可能是不同的头,或者不遵循重定向)。这将有助于了解问题所在以及如何解决问题。感谢您的回答。我知道有一些不同的标题