{"id":1187221,"date":"2025-01-15T20:02:40","date_gmt":"2025-01-15T12:02:40","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1187221.html"},"modified":"2025-01-15T20:02:42","modified_gmt":"2025-01-15T12:02:42","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e7%88%acqq%e7%a9%ba%e9%97%b4","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1187221.html","title":{"rendered":"\u5982\u4f55\u7528python\u722cqq\u7a7a\u95f4"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25135828\/18757328-4ab2-476b-95e4-ca0f68736f1d.webp\" alt=\"\u5982\u4f55\u7528python\u722cqq\u7a7a\u95f4\" \/><\/p>\n<p><p> <strong>\u8981\u7528Python\u722c\u53d6QQ\u7a7a\u95f4\uff0c\u53ef\u4ee5\u901a\u8fc7\u6a21\u62df\u767b\u5f55\u3001\u6293\u53d6\u6570\u636e\u3001\u5904\u7406\u6570\u636e\u7b49\u6b65\u9aa4\u6765\u5b9e\u73b0\u3002\u9996\u5148\u9700\u8981\u83b7\u53d6\u767b\u5f55\u6240\u9700\u7684Cookie\u548cToken\uff0c\u5e76\u5229\u7528\u5b83\u4eec\u8fdb\u884c\u6570\u636e\u8bf7\u6c42\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u6a21\u62df\u767b\u5f55<\/h3>\n<\/p>\n<p><p>\u8981\u8fdb\u884c\u722c\u53d6\uff0c\u9996\u5148\u9700\u8981\u6a21\u62df\u767b\u5f55QQ\u7a7a\u95f4\uff0c\u4ece\u800c\u83b7\u53d6Cookie\u548cToken\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>requests<\/code>\u5e93\u6765\u5b9e\u73b0\u8fd9\u4e00\u8fc7\u7a0b\u3002<\/p>\n<\/p>\n<p><h4>1. \u83b7\u53d6\u767b\u5f55\u53c2\u6570<\/h4>\n<\/p>\n<p><p>QQ\u7a7a\u95f4\u7684\u767b\u5f55\u9700\u8981QQ\u53f7\u548c\u5bc6\u7801\uff0c\u5e76\u4e14\u4f1a\u901a\u8fc7\u4e00\u7cfb\u5217\u8df3\u8f6c\u548c\u9a8c\u8bc1\u6765\u5b8c\u6210\u3002\u6211\u4eec\u9700\u8981\u83b7\u53d6\u767b\u5f55\u6240\u9700\u7684\u53c2\u6570\uff0c\u5305\u62ec\u9a8c\u8bc1\u7801\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>def get_login_params():<\/p>\n<p>    # \u83b7\u53d6\u767b\u5f55\u9875\u9762\uff0c\u5206\u6790\u9875\u9762\u5185\u5bb9\uff0c\u63d0\u53d6\u767b\u5f55\u53c2\u6570<\/p>\n<p>    login_url = &quot;https:\/\/qzone.qq.com\/&quot;<\/p>\n<p>    response = requests.get(login_url)<\/p>\n<p>    # \u5904\u7406\u54cd\u5e94\u5185\u5bb9\uff0c\u63d0\u53d6\u767b\u5f55\u53c2\u6570<\/p>\n<p>    # ...<\/p>\n<p>    return params<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u6a21\u62df\u767b\u5f55\u8bf7\u6c42<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7<code>requests<\/code>\u5e93\u53d1\u9001\u767b\u5f55\u8bf7\u6c42\uff0c\u83b7\u53d6\u767b\u5f55\u540e\u7684Cookie\u548cToken\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def login_qq(params):<\/p>\n<p>    login_api = &quot;https:\/\/ssl.ptlogin2.qq.com\/login&quot;<\/p>\n<p>    response = requests.post(login_api, data=params)<\/p>\n<p>    # \u63d0\u53d6\u5e76\u4fdd\u5b58Cookie\u548cToken<\/p>\n<p>    cookies = response.cookies<\/p>\n<p>    token = extract_token(response.text)<\/p>\n<p>    return cookies, token<\/p>\n<p>def extract_token(response_text):<\/p>\n<p>    # \u4ece\u54cd\u5e94\u6587\u672c\u4e2d\u63d0\u53d6token<\/p>\n<p>    # ...<\/p>\n<p>    return token<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u6293\u53d6\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u767b\u5f55\u6210\u529f\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528\u83b7\u53d6\u7684Cookie\u548cToken\u6765\u8bf7\u6c42QQ\u7a7a\u95f4\u7684\u9875\u9762\uff0c\u6293\u53d6\u6240\u9700\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>1. \u6784\u5efa\u8bf7\u6c42\u5934<\/h4>\n<\/p>\n<p><p>\u6784\u5efa\u8bf7\u6c42\u5934\uff0c\u5305\u542b\u5fc5\u8981\u7684Cookie\u548cToken\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def build_headers(cookies, token):<\/p>\n<p>    headers = {<\/p>\n<p>        &quot;Cookie&quot;: cookies,<\/p>\n<p>        &quot;Referer&quot;: &quot;https:\/\/user.qzone.qq.com\/&quot;,<\/p>\n<p>        &quot;User-Agent&quot;: &quot;Mozilla\/5.0&quot;<\/p>\n<p>    }<\/p>\n<p>    return headers<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u8bf7\u6c42QQ\u7a7a\u95f4\u9875\u9762<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528\u6784\u5efa\u597d\u7684\u8bf7\u6c42\u5934\uff0c\u53d1\u9001\u8bf7\u6c42\u83b7\u53d6QQ\u7a7a\u95f4\u7684\u9875\u9762\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def fetch_qzone_page(headers, user_id):<\/p>\n<p>    qzone_url = f&quot;https:\/\/user.qzone.qq.com\/{user_id}&quot;<\/p>\n<p>    response = requests.get(qzone_url, headers=headers)<\/p>\n<p>    return response.text<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u5904\u7406\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u6293\u53d6\u5230\u9875\u9762\u5185\u5bb9\u540e\uff0c\u9700\u8981\u5bf9\u6570\u636e\u8fdb\u884c\u5904\u7406\uff0c\u63d0\u53d6\u51fa\u9700\u8981\u7684\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><h4>1. \u89e3\u6790\u9875\u9762\u5185\u5bb9<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>BeautifulSoup<\/code>\u5e93\u89e3\u6790\u9875\u9762\u5185\u5bb9\uff0c\u63d0\u53d6\u51fa\u9700\u8981\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from bs4 import BeautifulSoup<\/p>\n<p>def parse_qzone_page(page_content):<\/p>\n<p>    soup = BeautifulSoup(page_content, &#39;html.parser&#39;)<\/p>\n<p>    # \u63d0\u53d6\u6240\u9700\u6570\u636e<\/p>\n<p>    # ...<\/p>\n<p>    return data<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4fdd\u5b58\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u5c06\u63d0\u53d6\u5230\u7684\u6570\u636e\u4fdd\u5b58\u5230\u6587\u4ef6\u6216\u6570\u636e\u5e93\u4e2d\uff0c\u4fbf\u4e8e\u540e\u7eed\u5206\u6790\u548c\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def save_data(data, file_path):<\/p>\n<p>    with open(file_path, &#39;w&#39;) as file:<\/p>\n<p>        file.write(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5b8c\u6574\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u5c06\u4e0a\u8ff0\u6b65\u9aa4\u6574\u5408\u5728\u4e00\u8d77\uff0c\u5f62\u6210\u5b8c\u6574\u7684\u722c\u53d6\u6d41\u7a0b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n():<\/p>\n<p>    user_id = &quot;\u76ee\u6807QQ\u53f7&quot;<\/p>\n<p>    params = get_login_params()<\/p>\n<p>    cookies, token = login_qq(params)<\/p>\n<p>    headers = build_headers(cookies, token)<\/p>\n<p>    page_content = fetch_qzone_page(headers, user_id)<\/p>\n<p>    data = parse_qzone_page(page_content)<\/p>\n<p>    save_data(data, &quot;qzone_data.txt&quot;)<\/p>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    main()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<ol>\n<li><strong>\u5408\u6cd5\u6027\u548c\u9690\u79c1<\/strong>\uff1a\u5728\u8fdb\u884c\u722c\u53d6\u65f6\uff0c\u4e00\u5b9a\u8981\u9075\u5b88\u76f8\u5173\u6cd5\u5f8b\u6cd5\u89c4\u548c\u7f51\u7ad9\u7684\u4f7f\u7528\u6761\u6b3e\uff0c\u907f\u514d\u4fb5\u72af\u7528\u6237\u9690\u79c1\u548c\u5229\u76ca\u3002<\/li>\n<li><strong>\u9a8c\u8bc1\u7801\u5904\u7406<\/strong>\uff1aQQ\u7a7a\u95f4\u767b\u5f55\u8fc7\u7a0b\u4e2d\u53ef\u80fd\u4f1a\u9047\u5230\u9a8c\u8bc1\u7801\uff0c\u9700\u8981\u4f7f\u7528\u56fe\u50cf\u8bc6\u522b\u6280\u672f\u6216\u624b\u52a8\u8f93\u5165\u9a8c\u8bc1\u7801\u8fdb\u884c\u5904\u7406\u3002<\/li>\n<li><strong>\u53cd\u722c\u866b\u673a\u5236<\/strong>\uff1aQQ\u7a7a\u95f4\u6709\u53cd\u722c\u866b\u673a\u5236\uff0c\u53ef\u80fd\u4f1a\u5bf9\u9891\u7e41\u8bf7\u6c42\u8fdb\u884c\u9650\u5236\uff0c\u9700\u8981\u5408\u7406\u8bbe\u7f6e\u8bf7\u6c42\u9891\u7387\u548c\u5904\u7406\u5f02\u5e38\u60c5\u51b5\u3002<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u53ef\u4ee5\u5b9e\u73b0\u7528Python\u722c\u53d6QQ\u7a7a\u95f4\u7684\u529f\u80fd\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0cQQ\u7a7a\u95f4\u7684\u53cd\u722c\u866b\u673a\u5236\u8f83\u4e3a\u4e25\u683c\uff0c\u5b9e\u9645\u64cd\u4f5c\u4e2d\u53ef\u80fd\u4f1a\u9047\u5230\u5404\u79cd\u95ee\u9898\uff0c\u9700\u8981\u6839\u636e\u5177\u4f53\u60c5\u51b5\u8fdb\u884c\u8c03\u6574\u548c\u4f18\u5316\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5f00\u59cb\u4f7f\u7528Python\u722c\u53d6QQ\u7a7a\u95f4\u7684\u6570\u636e\uff1f<\/strong><br \/>\u8981\u5f00\u59cb\u7528Python\u722c\u53d6QQ\u7a7a\u95f4\uff0c\u9996\u5148\u9700\u8981\u5b89\u88c5\u4e00\u4e9b\u57fa\u7840\u5e93\uff0c\u5982requests\u548cBeautifulSoup\u3002requests\u5e93\u53ef\u4ee5\u5e2e\u52a9\u4f60\u53d1\u9001HTTP\u8bf7\u6c42\uff0c\u800cBeautifulSoup\u5219\u7528\u4e8e\u89e3\u6790\u7f51\u9875\u5185\u5bb9\u3002\u786e\u4fdd\u4f60\u4e86\u89e3QQ\u7a7a\u95f4\u7684\u7ed3\u6784\u548c\u6570\u636e\u683c\u5f0f\uff0c\u8fd9\u6837\u53ef\u4ee5\u66f4\u6709\u6548\u5730\u63d0\u53d6\u6240\u9700\u4fe1\u606f\u3002\u6b64\u5916\uff0c\u4f7f\u7528\u722c\u866b\u524d\uff0c\u5efa\u8bae\u4f60\u4ed4\u7ec6\u9605\u8bfbQQ\u7a7a\u95f4\u7684\u4f7f\u7528\u6761\u6b3e\uff0c\u786e\u4fdd\u4e0d\u8fdd\u53cd\u76f8\u5173\u89c4\u5b9a\u3002<\/p>\n<p><strong>\u5728\u722c\u53d6QQ\u7a7a\u95f4\u65f6\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u6cd5\u5f8b\u548c\u9053\u5fb7\u95ee\u9898\uff1f<\/strong><br \/>\u722c\u53d6\u4efb\u4f55\u7f51\u7ad9\u7684\u6570\u636e\u65f6\uff0c\u9075\u5faa\u6cd5\u5f8b\u548c\u9053\u5fb7\u89c4\u8303\u662f\u81f3\u5173\u91cd\u8981\u7684\u3002\u786e\u4fdd\u4e0d\u4fb5\u72af\u7528\u6237\u7684\u9690\u79c1\u6743\uff0c\u4e0d\u6536\u96c6\u654f\u611f\u4fe1\u606f\u3002\u540c\u65f6\uff0c\u9075\u5b88robots.txt\u6587\u4ef6\u4e2d\u7684\u89c4\u5b9a\uff0c\u907f\u514d\u5bf9\u670d\u52a1\u5668\u9020\u6210\u8d1f\u62c5\u3002\u6b64\u5916\uff0c\u5efa\u8bae\u53ea\u5728\u4e2a\u4eba\u5b66\u4e60\u548c\u7814\u7a76\u7684\u8303\u56f4\u5185\u4f7f\u7528\u722c\u53d6\u7684\u6570\u636e\uff0c\u5207\u52ff\u5c06\u5176\u7528\u4e8e\u5546\u4e1a\u76ee\u7684\u6216\u4f20\u64ad\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406QQ\u7a7a\u95f4\u4e2d\u7684\u52a8\u6001\u5185\u5bb9\u548c\u53cd\u722c\u866b\u63aa\u65bd\uff1f<\/strong><br \/>QQ\u7a7a\u95f4\u53ef\u80fd\u4f7f\u7528JavaScript\u52a0\u8f7d\u90e8\u5206\u5185\u5bb9\uff0c\u8fd9\u610f\u5473\u7740\u4ec5\u4ec5\u901a\u8fc7\u9759\u6001\u722c\u53d6\u65e0\u6cd5\u83b7\u53d6\u6240\u6709\u4fe1\u606f\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528selenium\u7b49\u5de5\u5177\u6a21\u62df\u6d4f\u89c8\u5668\u884c\u4e3a\uff0c\u83b7\u53d6\u52a8\u6001\u52a0\u8f7d\u7684\u6570\u636e\u3002\u540c\u65f6\uff0c\u8981\u6ce8\u610fQQ\u7a7a\u95f4\u53ef\u80fd\u4f1a\u6709\u53cd\u722c\u866b\u673a\u5236\uff0c\u4f8b\u5982IP\u5c01\u7981\u3001\u9a8c\u8bc1\u7801\u7b49\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbe\u7f6e\u8bf7\u6c42\u95f4\u9694\u3001\u4f7f\u7528\u4ee3\u7406\u7b49\u65b9\u5f0f\u6765\u51cf\u5c11\u88ab\u68c0\u6d4b\u7684\u98ce\u9669\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u7528Python\u722c\u53d6QQ\u7a7a\u95f4\uff0c\u53ef\u4ee5\u901a\u8fc7\u6a21\u62df\u767b\u5f55\u3001\u6293\u53d6\u6570\u636e\u3001\u5904\u7406\u6570\u636e\u7b49\u6b65\u9aa4\u6765\u5b9e\u73b0\u3002\u9996\u5148\u9700\u8981\u83b7\u53d6\u767b\u5f55\u6240\u9700\u7684Cook [&hellip;]","protected":false},"author":3,"featured_media":1187224,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1187221"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1187221"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1187221\/revisions"}],"predecessor-version":[{"id":1187225,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1187221\/revisions\/1187225"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1187224"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1187221"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1187221"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1187221"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}