Taking the requests library as an example, open cmd and run the command pip install requests
to install the requests library. Since I have already installed it, it will be prompted. Requirement already satisfied
At this point , we use Pycharm to run the following code:
import requests
url = 'https://www.baidu.com/s'
data = {
'ie': 'utf8',
'kw': '中国',
}
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36',
}
r = requests.get(url, headers=headers, params=data)
with open('baidu.html', 'wb') as fp:
fp.write(r.content)
There is no problem with the code, but an error will be reported: ModuleNotFoundError: No module named 'requests'
Solution: Select [ File ]-[ Settings ] in turn, find the current project, select [ Project Interpreter ], and in the list on the right, you can see the various libraries that have been installed, The corresponding version and the latest version, and then click the + sign to search for the library that reported the error, such as the requests library in this article. After selecting it, click [ Install Package ] to install the library. After the installation is successful, there will be a prompt Packages installed successfully in the lower right corner. Run the program again. No error reported!