【发布时间】:2016-03-16 15:16:00
【问题描述】:
我正在尝试使用 pythons 的 requests 库从 php 脚本中检索 html 内容。
该脚本驻留在我的本地 Apache 服务器中,我直接在以下位置访问它:http://localhost/aaa/index.php
脚本的内容是:
<?php
$headers = json_encode(apache_request_headers());
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>The Title</title>
<meta name="description" content="The Title">
</head>
<body>
<?php echo json_encode($headers); ?>
</body>
</html>
直接访问上述脚本会产生如下响应:
<head>
<meta charset="utf-8">
<title>The Title</title>
<meta name="description" content="The Title">
</head>
<body>
"{\"Host\":\"localhost\",\"User-Agent\":\"Mozilla\\\/5.0 (Windows NT 6.3; WOW64; rv:42.0) Gecko\\\
/20100101 Firefox\\\/42.0\",\"Accept\":\"text\\\/html,application\\\/xhtml+xml,application\\\/xml;q=0
.9,*\\\/*;q=0.8\",\"Accept-Language\":\"en-US,en;q=0.5\",\"Accept-Encoding\":\"gzip, deflate\",\"Cookie
\":\"menu=users%3Bconfiguration; fieldset=; PHPSESSID=tn82odn5hdtr45mw0bkd6rhf56; nr
=5c3ab462abb1d3364b8ba59fa4d8b7f6; ru=popopo; rp=64864wb5630986rgn5860f52vy0614909b8a8736
\",\"Connection\":\"keep-alive\",\"Cache-Control\":\"max-age=0\"}"
</body>
</html>
当我使用 Python 访问上述网址 [http://localhost/aaa/index.php] 时,我得到了不同的响应。
Python 代码:
import requests
url = "http://localhost/aaa/index.php"
headers = {'User-Agent': 'Mozilla/5.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; GTB7.4; InfoPath.2; SV1; .NET CLR 3.3.69573; WOW64; en-US)',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'en-US,en;q=0.5',
'Connection': 'Keep-Alive',
'Content-Type': 'text/html; charset=UTF-8'}
req = requests.get(url, headers=headers)
print("Body :::", req.content)
然后回应:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>The Title</title>
<meta name="description" content="The Title">
</head>
<body>
"{\\"Host\\":\\"localhost\\",\\"Accept-Encoding\\":\\"gzip,
deflate\\",\\"Accept-Language\\":\\"en-US,en;q=0.5\\",
\\"Accept-Charset\\":\\"ISO-8859-1,utf-8;q=0.7,*;q=0.3\\",
\\"User-Agent\\":\\"Mozilla\\\\\\/5.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident
\\\\\\/4.0; GTB7.4; InfoPath.2; SV1; .NET CLR 3.3.69573; WOW64; en-US)\\",\\"Accept\\":\\"text\\\\\\/html,application
\\\\\\/xhtml+xml,application\\\\\\/xml;q=0.9,*
\\\\\\/*;q=0.8\\",\\"Connection\\":\\"Keep-Alive
\\",\\"Content-Type\\":\\"text\\\\\\/html; charset=UTF-8\\"}"
</body>
</html>
请注意,当我使用 Python 请求资源时,“Cookie”丢失了。 cookie 是我真正想要检索的。我需要它,以便从其他 php 页面读取内容。
我也尝试了以下方法但没有成功:
import requests
url = "http://localhost/aaa/index.php"
session = requests.Session()
session.cookies.get_dict()
response = session.get(url, headers=headers)
print("Cookies :::", session.cookies.get_dict())
有什么方法可以帮到你吗?
【问题讨论】:
-
你试过看
req.cookies吗? -
@Morgan Thrapp 是的,cookiejar 是空的
-
@Darth Vader 我试过了,但我得到了空的 cookiejar
-
“上述脚本的直接访问会产生以下响应:” ...不完全是。您做了一些事情让服务器向您发送该 cookie,现在您的浏览器正在将它与每个请求一起发送回来。如果您从浏览器中清除 localhost 的所有 cookie,您将不会再看到它。
标签: php python apache session cookies