a="""
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 导入所需模块\n",
    "import requests\n",
    "import re\n",
    "import os\n",
    "from urllib.request import urlretrieve"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'https://xueshu.baidu.com/s?wd=Python&ie=utf-8&tn=SE_baiduxueshu_c1gjeupa&sc_from=&sc_as_para=sc_lib%3A&rsv_sug2=0'"
      ]
     },
     "execution_count": 78,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获取URL信息\n",
    "def get_url(key):\n",
    "\n",
    "    url = 'https://xueshu.baidu.com/s?wd=' + key + '&ie=utf-8&tn=SE_baiduxueshu_c1gjeupa&sc_from=&sc_as_para=sc_lib%3A&rsv_sug2=0'\n",
    "    return url\n",
    "\n",
    "\n",
    "get_url('Python')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 设置请求头\n",
    "headers = {\n",
    "    'user-agent':\n",
    "    'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36',\n",
    "    'Referer': 'https://googleads.g.doubleclick.net/'\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 获取相关论文的DOI列表\n",
    "def get_paper_link(headers, key):\n",
    "    response = requests.get(url=get_url(key), headers=headers)\n",
    "    res1_data = response.text\n",
    "    #找论文链接\n",
    "    paper_link = re.findall(r'<h3 class=\\\"t c_font\\\">\\n +\\n +<a href=\\\"(.*)\\\"',\n",
    "                            res1_data)\n",
    "\n",
    "    doi_list = []  #用一个列表接收论文的DOI\n",
    "    for link in paper_link:\n",
    "        paper_link = 'http:' + link\n",
    "        response2 = requests.get(url=paper_link, headers=headers)\n",
    "        res2_data = response2.text\n",
    "        #提取论文的DOI\n",
    "        try:\n",
    "            paper_doi = re.findall(r'\\'doi\\'}\\\">\\n +(.*?)\\n ', res2_data)\n",
    "            if str(10) in paper_doi[0]:\n",
    "                doi_list.append(paper_doi)\n",
    "\n",
    "        except:\n",
    "            pass\n",
    "    return doi_list\n",
    "\n",
    "\n",
    "a = get_paper_link(headers,'apple')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {},
   "outputs": [],
   "source": [
    "#构建sci-hub下载链接\n",
    "def doi_download(headers, key):\n",
    "    doi_list = get_paper_link(headers, key)\n",
    "    lst = []\n",
    "    for i in doi_list:\n",
    "        lst.append(list(i[0]))\n",
    "    for i in lst:\n",
    "        for j in range(8, len(i)):\n",
    "            if i[j] == '/':\n",
    "                i[j] = '%252F'\n",
    "            elif i[j] == '(':\n",
    "                i[j] = '%2528'\n",
    "            elif i[j] == ')':\n",
    "                i[j] = '%2529'\n",
    "            else:\n",
    "                i[j] = i[j].lower()\n",
    "    for i in range(len(lst)):\n",
    "        lst[i] = ''.join(lst[i])\n",
    "    for doi in lst:\n",
    "        down_link = 'https://sci.bban.top/pdf/' + doi + '.pdf'\n",
    "        print(down_link)\n",
    "        file_name = doi.split('/')[-1] + '.pdf'\n",
    "        try:\n",
    "            with open(file_name, 'wb') as f:\n",
    "                r = requests.get(url=down_link, headers=headers)\n",
    "                f.write(r.content)\n",
    "                print('下载完毕：' + file_name)\n",
    "        except:\n",
    "            print(\"该文章为空\")\n",
    "            pass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# 检索及下载\n",
    "key = input(\"请输入您想要下载论文的关键词（英文）：\")\n",
    "doi_download(headers, key)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  },
  "latex_envs": {
   "LaTeX_envs_menu_present": true,
   "autoclose": false,
   "autocomplete": true,
   "bibliofile": "biblio.bib",
   "cite_by": "apalike",
   "current_citInitial": 1,
   "eqLabelWithNumbers": true,
   "eqNumInitial": 1,
   "hotkeys": {
    "equation": "Ctrl-E",
    "itemize": "Ctrl-I"
   },
   "labels_anchors": false,
   "latex_user_defs": false,
   "report_style_numbering": false,
   "user_envs_cfg": false
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
"""
print(a)