{"id":1654,"date":"2020-06-22T01:25:17","date_gmt":"2020-06-21T17:25:17","guid":{"rendered":"http:\/\/39.97.231.10\/?p=1654"},"modified":"2020-06-22T01:25:17","modified_gmt":"2020-06-21T17:25:17","slug":"%e4%bd%bf%e7%94%a8requests%e7%88%ac%e5%8f%96%e6%8b%89%e5%8b%be%e7%bd%91python%e8%81%8c%e4%bd%8d%e6%95%b0%e6%8d%ae","status":"publish","type":"post","link":"http:\/\/www.weijing.co\/?p=1654","title":{"rendered":"\u4f7f\u7528requests\u722c\u53d6\u62c9\u52fe\u7f51python\u804c\u4f4d\u6570\u636e"},"content":{"rendered":"<h1>\u4f7f\u7528requests\u722c\u53d6\u62c9\u52fe\u7f51python\u804c\u4f4d\u6570\u636e<\/h1>\n<div>\n<div><\/div>\n<\/div>\n<article>\n<h4>\u722c\u866b\u76ee\u7684<\/h4>\n<p>\u672c\u6587\u662f\u60f3\u901a\u8fc7\u722c\u53d6\u62c9\u52fe\u7f51Python\u76f8\u5173\u5c97\u4f4d\u6570\u636e\uff0c\u7b80\u5355\u68b3\u7406Requests\u548cxpath\u7684\u4f7f\u7528\u65b9\u6cd5\u3002<br \/>\n\u4ee3\u7801\u90e8\u5206\u5e76\u6ca1\u6709\u505a\u5c01\u88c5\uff0c\u6570\u636e\u8bf7\u6c42\u4e5f\u6bd4\u8f83\u7b80\u5355\uff0c\u6240\u4ee5\u8be5\u9879\u76ee\u53ea\u662f\u4e3a\u4e86\u719f\u6089requests\u722c\u866b\u7684\u57fa\u672c\u539f\u7406\uff0c\u65e0\u6cd5\u7528\u4e8e\u7a33\u5b9a\u7684\u722c\u866b\u9879\u76ee\u3002<\/p>\n<h4>\u722c\u866b\u5de5\u5177<\/h4>\n<p>\u8fd9\u6b21\u4f7f\u7528Requests\u5e93\u53d1\u9001http\u8bf7\u6c42\uff0c\u7136\u540e\u7528lxml.etree\u89e3\u6790HTML\u6587\u6863\u5bf9\u8c61\uff0c\u5e76\u4f7f\u7528xpath\u83b7\u53d6\u804c\u4f4d\u4fe1\u606f\u3002<br \/>\nRequests\u7b80\u4ecb<br \/>\nRequests\u662f\u4e00\u6b3e\u76ee\u524d\u975e\u5e38\u6d41\u884c\u7684http\u8bf7\u6c42\u5e93\uff0c\u4f7f\u7528python\u7f16\u5199\uff0c\u80fd\u975e\u5e38\u65b9\u4fbf\u7684\u5bf9\u7f51\u9875Requests\u8fdb\u884c\u722c\u53d6\u3002<\/p>\n<blockquote><p>\u5b98\u7f51\u91cc\u4ecb\u7ecd\u8bf4\uff1aRequests is an elegant and simple HTTP library for Python, built for human beings.<br \/>\nRequests\u4f18\u96c5\u3001\u7b80\u6613\uff0c\u4e13\u4e3a\u4eba\u7c7b\u6253\u9020\uff01<\/p><\/blockquote>\n<p>\u603b\u800c\u8a00\u4e4b\uff0cRequests\u7528\u8d77\u6765\u7b80\u5355\u987a\u624b\u3002<br \/>\nRequests\u5e93\u53ef\u4ee5\u4f7f\u7528pip\u6216\u8005conda\u5b89\u88c5\uff0c\u672c\u6587python\u73af\u5883\u4e3apy3.6\u3002<br \/>\n\u8bd5\u8bd5\u5bf9\u767e\u5ea6\u9996\u9875\u8fdb\u884c\u6570\u636e\u8bf7\u6c42\uff1a<\/p>\n<div>\n<pre># \u5bfc\u5165requests\u6a21\u5757\nimport requests&lt;br&gt;\n# \u53d1\u51fahttp\u8bf7\u6c42\nre = requests.get(\\\"https:\/\/www.baidu.com\/\\\")\n# \u67e5\u770b\u54cd\u5e94\u72b6\u6001\nprint(re.status_code)\n# \u67e5\u770burl\nprint(re.url)\n# \u67e5\u770b\u54cd\u5e94\u5185\u5bb9\nprint(re.text)\n# \u67e5\u770b\u7f16\u7801\nprint(re.encoding)\n# \u4e8c\u8fdb\u5236\u54cd\u5e94\u5185\u5bb9\nprint(re.content)\n# json\u54cd\u5e94\u5185\u5bb9\nprint(re.json)\n<\/pre>\n<\/div>\n<p>xpath\u7b80\u4ecb<br \/>\nxpath \u662f\u4e00\u95e8\u5728 XML \u6587\u6863\u4e2d\u67e5\u627e\u4fe1\u606f\u7684\u8bed\u8a00\uff0c\u53ef\u7528\u6765\u5728 XML \u6587\u6863\u4e2d\u5bf9\u5143\u7d20\u548c\u5c5e\u6027\u8fdb\u884c\u904d\u5386\u3002<br \/>\n\u5bf9\u83b7\u53d6\u7684\u6570\u636e\u8fdb\u884c\u89e3\u6790\u9700\u8981\u7528\u5230lxml\u5e93\uff0clxml\u5e93\u662fPython\u7684\u4e00\u4e2a\u89e3\u6790\u5e93\uff0c\u652f\u6301HTML\u548cXML\u7684\u89e3\u6790\uff0c\u652f\u6301XPath\u3002<\/p>\n<h4>\u5f00\u59cb\u6570\u636e\u91c7\u96c6<\/h4>\n<p>1\u3001\u8bf7\u6c42\u5730\u5740\uff1a<br \/>\nhttps:\/\/www.lagou.com\/zhaopin\/Python\/<\/p>\n<div>\n<div>\n<div><\/div>\n<div data-width=\\\"1899\\\" data-height=\\\"975\\\"><img src=\\\"https:\/\/upload-images.jianshu.io\/upload_images\/13723999-54f078944d81988e.png?imageMogr2\/auto-orient\/strip|imageView2\/2\/w\/1200\/format\/webp\\\" data-original-src=\\\"\/\/upload-images.jianshu.io\/upload_images\/13723999-54f078944d81988e.png\\\" data-original-width=\\\"1899\\\" data-original-height=\\\"975\\\" data-original-format=\\\"image\/png\\\" data-original-filesize=\\\"438813\\\" data-image-index=\\\"0\\\" \/><\/div>\n<\/div>\n<\/div>\n<p>2\u3001\u9700\u8981\u722c\u53d6\u7684\u5185\u5bb9<br \/>\n\u83b7\u53d6\u804c\u4f4d\u6982\u51b5\u4fe1\u606f\uff0c\u5305\u62ec\uff1a<\/p>\n<ul>\n<li>\u804c\u4f4d\u540d\u79f0<\/li>\n<li>\u516c\u53f8\u540d\u79f0<\/li>\n<li>\u516c\u53f8\u7b80\u4ecb<\/li>\n<li>\u85aa\u6c34<\/li>\n<li>\u804c\u4f4d\u62db\u8058\u5bf9\u8c61<\/li>\n<li>\u5de5\u4f5c\u804c\u8d23<\/li>\n<li>\u5de5\u4f5c\u8981\u6c42<\/li>\n<\/ul>\n<p>3\u3001\u67e5\u770bhtml<br \/>\n\u5982\u679c\u4f60\u4f7f\u7528chrome\u6d4f\u89c8\u5668\uff0c\u767b\u9646\u62c9\u52fe\u7f51\uff0c\u6309F12\u53ef\u4ee5\u8fdb\u5165\u5f00\u53d1\u8005\u5de5\u5177\u9875\u9762\uff1a<\/p>\n<div>\n<div>\n<div><\/div>\n<div data-width=\\\"1902\\\" data-height=\\\"971\\\"><img src=\\\"https:\/\/upload-images.jianshu.io\/upload_images\/13723999-76eec6bf0781d614.png?imageMogr2\/auto-orient\/strip|imageView2\/2\/w\/1200\/format\/webp\\\" data-original-src=\\\"\/\/upload-images.jianshu.io\/upload_images\/13723999-76eec6bf0781d614.png\\\" data-original-width=\\\"1902\\\" data-original-height=\\\"971\\\" data-original-format=\\\"image\/png\\\" data-original-filesize=\\\"311061\\\" data-image-index=\\\"1\\\" \/><\/div>\n<\/div>\n<\/div>\n<p>\u8fd9\u65f6\u5019\u4f60\u4f1a\u770b\u5230\u8be5\u9875\u9762\u7684html\u7f51\u9875\u6e90\u7801\u3002<br \/>\n\u63a5\u4e0b\u6765\u9700\u8981\u5bfb\u627e\u5c97\u4f4d\u4fe1\u606f\u5bf9\u5e94\u7684\u6e90\u7801\uff0c\u6bd4\u5982\u8bf4\u6211\u60f3\u8981\u83b7\u53d6\u804c\u4f4d\u540d\u79f0\uff1a<\/p>\n<div>\n<div>\n<div><\/div>\n<div data-width=\\\"951\\\" data-height=\\\"182\\\"><img src=\\\"https:\/\/upload-images.jianshu.io\/upload_images\/13723999-bac2910d090089bd.png?imageMogr2\/auto-orient\/strip|imageView2\/2\/w\/951\/format\/webp\\\" data-original-src=\\\"\/\/upload-images.jianshu.io\/upload_images\/13723999-bac2910d090089bd.png\\\" data-original-width=\\\"951\\\" data-original-height=\\\"182\\\" data-original-format=\\\"image\/png\\\" data-original-filesize=\\\"21145\\\" data-image-index=\\\"2\\\" \/><\/div>\n<\/div>\n<\/div>\n<p>\u4f60\u4f1a\u770b\u5230\u5f00\u53d1\u8005\u5de5\u5177\u9875\u9762\u5de6\u4e0a\u89d2\u6709\u4e2a\u7bad\u5934\u6807\u5fd7\uff0c\u4f60\u9700\u8981\u70b9\u51fb\u5b83\uff0c\u7136\u540e\u518d\u70b9\u51fb\u5c97\u4f4d\u540d\u79f0\uff0c\u5c31\u80fd\u770b\u5230\u5bf9\u5e94\u7684\u6e90\u7801\u3002<\/p>\n<div>\n<div>\n<div><\/div>\n<div data-width=\\\"1895\\\" data-height=\\\"972\\\"><img src=\\\"https:\/\/upload-images.jianshu.io\/upload_images\/13723999-507b3ff45cbecbd0.png?imageMogr2\/auto-orient\/strip|imageView2\/2\/w\/1200\/format\/webp\\\" data-original-src=\\\"\/\/upload-images.jianshu.io\/upload_images\/13723999-507b3ff45cbecbd0.png\\\" data-original-width=\\\"1895\\\" data-original-height=\\\"972\\\" data-original-format=\\\"image\/png\\\" data-original-filesize=\\\"353720\\\" data-image-index=\\\"3\\\" \/><\/div>\n<\/div>\n<\/div>\n<p>\u77e5\u9053\u5bf9\u5e94\u7684\u6e90\u7801\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5229\u7528xpath\u63d0\u53d6\u91cc\u9762\u7684\u6587\u672c\u30024\u3001\u5229\u7528requests\u53d1\u51fa\u6570\u636e\u8bf7\u6c42<\/p>\n<div>\n<pre># \u8bf7\u6c42\u5934\nheaders = {\n        \\'User-Agent\\':\\'Mozilla\/5.0 (Windows NT 6.1; WOW64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/47.0.2526.80 Safari\/537.36 Core\/1.47.933.400 QQBrowser\/9.4.8699.400\\',\n    }\n# \u53d1\u51fa\u6570\u636e\u8bf7\u6c42\uff0c\u8fd4\u56deresponse\u54cd\u5e94\u5bf9\u8c61\nr = requests.get(\\'https:\/\/www.lagou.com\/zhaopin\/Python\/\\',headers=headers)\n# \u8fdb\u884cutf-8\u7f16\u7801\nr.encoding = \\\"utf-8\\\"\n<\/pre>\n<\/div>\n<p>5\u3001\u5229\u7528xpath\u63d0\u53d6\u6570\u636e<\/p>\n<div>\n<pre># \u6784\u9020\u4e00\u4e2axpath\u89e3\u6790\u5bf9\u8c61\nselector = etree.HTML(r.text)\n# \u804c\u4f4d\u7f16\u53f7\uff0c\u4e00\u9875\u5171\u670914\u4e2a\u804c\u4f4d\uff0c\u6240\u4ee5\u8fd9\u91cc\u53ef\u4ee5\u4f20\u90120~13\u4efb\u610f\u6570\u5b57\nrow_num = str(0)\n\\'\\'\\'\u83b7\u53d6\u804c\u4f4d\u6982\u51b5\u4fe1\u606f\uff0c\u5305\u62ec\uff1a\u804c\u4f4d\u540d\u79f0\u3001\u516c\u53f8\u540d\u79f0\u3001\u516c\u53f8\u7b80\u4ecb\u3001\u85aa\u6c34\u3001\u804c\u4f4d\u62db\u8058\u5bf9\u8c61\\'\\'\\'\n# p_name \u804c\u4f4d\u540d\u79f0\np_name = selector.xpath(\\'\/\/ul[@class=\\\"item_con_list\\\"]\/li[@data-index={}]\/\/h3\/text()\\'.format(row_num))[0]\n# p_company \u62db\u8058\u516c\u53f8\u540d\u79f0\np_company = selector.xpath(\\'\/\/ul[@class=\\\"item_con_list\\\"]\/li[@data-index={}]\/\/div[@class=\\\"company_name\\\"]\/a\/text()\\'.format(row_num))[0]\n# p_industry \u62db\u8058\u516c\u53f8\u7b80\u4ecb\np_industry = selector.xpath(\\'\/\/ul[@class=\\\"item_con_list\\\"]\/li[@data-index={}]\/\/div[@class=\\\"industry\\\"]\/text()\\'.format(row_num))[0]\n# p_money \u804c\u4f4d\u85aa\u8d44\np_money = selector.xpath(\\'\/\/ul[@class=\\\"item_con_list\\\"]\/li[@data-index={}]\/\/span[@class=\\\"money\\\"]\/text()\\'.format(row_num))[0]\n# p_require \u804c\u4f4d\u62db\u8058\u5bf9\u8c61\np_require = selector.xpath(\\'\/\/ul[@class=\\\"item_con_list\\\"]\/li[@data-index={}]\/\/div[@class=\\\"li_b_l\\\"]\/text()\\'.format(row_num))[2]\n# \u8be5\u804c\u4f4d\u8be6\u7ec6\u4fe1\u606f\u8df3\u8f6c\u94fe\u63a5\np_href = selector.xpath(\\'\/\/ul[@class=\\\"item_con_list\\\"]\/li[@data-index={}]\/\/a[@class=\\\"position_link\\\"]\/@href\\'.format(row_num))[0]\n<\/pre>\n<\/div>\n<p>\u5c97\u4f4d\u804c\u8d23\u548c\u8981\u6c42\u7684\u4fe1\u606f\u5728\u53e6\u5916\u7684\u7f51\u9875\uff0c\u6240\u4ee5\u8981\u5148\u83b7\u53d6\u5bf9\u5e94\u7f51\u5740\uff0c\u518d\u8fdb\u884c\u6570\u636e\u8bf7\u6c42\u3002<\/p>\n<div>\n<pre>\\'\\'\\' \u83b7\u53d6\u5de5\u4f5c\u804c\u8d23\u548c\u5de5\u4f5c\u8981\u6c42\u7b49\u8be6\u7ec6\u4fe1\u606f\\'\\'\\'\n# \u8fd4\u56deresponse\u54cd\u5e94\u5bf9\u8c61\nr_detail = requests.get(p_href,headers=headers)\n# \u8fdb\u884cutf-8\u7f16\u7801\nr_detail.encoding = \\\"utf-8\\\"\n# \u89e3\u6790HTML\u5bf9\u8c61\nselector_detail = etree.HTML(r_detail.text)\n# p_detail \u5de5\u4f5c\u804c\u8d23\u548c\u5de5\u4f5c\u8981\u6c42\u4fe1\u606f\np_detail = selector_detail.xpath(\\'\/\/div[@class=\\\"job-detail\\\"]\/p\/\/text()\\')\n<\/pre>\n<\/div>\n<p>\u5c06\u6240\u6709\u4fe1\u606f\u653e\u8fdb\u5217\u8868\u5e76\u6253\u5370\uff1a<\/p>\n<div>\n<pre>\\'\\'\\'\u5c06\u83b7\u53d6\u5230\u7684\u6709\u6548\u4fe1\u606f\u653e\u5230\u5217\u8868\u4e2d\\'\\'\\'\np_list = [p_name, p_company, p_industry.strip(), p_money, p_require.strip(),\\\"\\\\n \\\".join(p_detail)]\nfor i in p_list:\n    print(i)\n<\/pre>\n<\/div>\n<p>\u8f93\u51fa\uff1a<\/p>\n<div>\n<div>\n<div><\/div>\n<div data-width=\\\"925\\\" data-height=\\\"697\\\"><img src=\\\"https:\/\/upload-images.jianshu.io\/upload_images\/13723999-9d83ca735a655098.png?imageMogr2\/auto-orient\/strip|imageView2\/2\/w\/925\/format\/webp\\\" data-original-src=\\\"\/\/upload-images.jianshu.io\/upload_images\/13723999-9d83ca735a655098.png\\\" data-original-width=\\\"925\\\" data-original-height=\\\"697\\\" data-original-format=\\\"image\/png\\\" data-original-filesize=\\\"115696\\\" data-image-index=\\\"4\\\" \/><\/div>\n<\/div>\n<\/div>\n<h4>\u603b\u7ed3<\/h4>\n<p>\u672c\u6587\u4f7f\u7528requests\u548cxpath\u5de5\u5177\u5bf9\u62c9\u52fe\u7f51python\u804c\u4f4d\u6570\u636e\u8fdb\u884c\u722c\u53d6\u5e76\u89e3\u6790\uff0c\u65e8\u5728\u4e86\u89e3requests\u548cxpath\u7684\u4f7f\u7528\u65b9\u6cd5\u3002<br \/>\n<\/article>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528requests\u722c\u53d6\u62c9\u52fe\u7f51python\u804c\u4f4d\u6570\u636e \u722c\u866b\u76ee\u7684 \u672c\u6587\u662f\u60f3\u901a\u8fc7\u722c\u53d6\u62c9\u52fe\u7f51Python\u76f8\u5173\u5c97\u4f4d\u6570\u636e\uff0c &hellip;<\/p>\n<p class=\"read-more\"> <a class=\"\" href=\"http:\/\/www.weijing.co\/?p=1654\"> <span class=\"screen-reader-text\">\u4f7f\u7528requests\u722c\u53d6\u62c9\u52fe\u7f51python\u804c\u4f4d\u6570\u636e<\/span> \u67e5\u770b\u5168\u6587 &raquo;<\/a><\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"default","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"default","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":""},"categories":[106,126],"tags":[],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false,"mailpoet_newsletter_max":false,"woocommerce_thumbnail":false,"woocommerce_single":false,"woocommerce_gallery_thumbnail":false,"shop_catalog":false,"shop_single":false,"shop_thumbnail":false},"uagb_author_info":{"display_name":"weijing","author_link":"http:\/\/www.weijing.co\/?author=4"},"uagb_comment_info":0,"uagb_excerpt":"\u4f7f\u7528requests\u722c\u53d6\u62c9\u52fe\u7f51python\u804c\u4f4d\u6570\u636e \u722c\u866b\u76ee\u7684 \u672c\u6587\u662f\u60f3\u901a\u8fc7\u722c\u53d6\u62c9\u52fe\u7f51Python\u76f8\u5173\u5c97\u4f4d\u6570\u636e\uff0c&hellip;","_links":{"self":[{"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/posts\/1654"}],"collection":[{"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"http:\/\/www.weijing.co\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1654"}],"version-history":[{"count":0,"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/posts\/1654\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.weijing.co\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1654"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.weijing.co\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1654"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.weijing.co\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1654"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}