闲来无事学python(爬虫),使用任何软件的第一步就是正确地安装它,安装requests模块报错
Collecting requests
Using cached https://files.pythonhosted.org/packages/7d/e3/20f3d364d6c8e5d2353c72a67778eb189176f08e873c9900e10c0287b84b/requests-2.21.0-py2.py3-none-any.whl
Collecting idna<2.9,>=2.5 (from requests)
Downloading https://files.pythonhosted.org/packages/14/2c/cd551d81dbe15200be1cf41cd03869a46fe7226e7450af7a6545bfc474c9/idna-2.8-py2.py3-none-any.whl (58kB)
100% |████████████████████████████████| 61kB 9.7kB/s
Collecting urllib3<1.25,>=1.21.1 (from requests)
Downloading https://files.pythonhosted.org/packages/62/00/ee1d7de624db8ba7090d1226aebefab96a2c71cd5cfa7629d6ad3f61b79e/urllib3-1.24.1-py2.py3-none-any.whl (118kB)
69% |██████████████████████ | 81kB 2.2kB/s eta 0:00:17Exception:
Traceback (most recent call last):
File "d:\tan\soft\python3.7.3\lib\site-packages\pip\_vendor\urllib3\response.py", line 360, in _error_catcher
yield
File "d:\tan\soft\python3.7.3\lib\site-packages\pip\_vendor\urllib3\response.py", line 442, in read
data = self._fp.read(amt)
File "d:\tan\soft\python3.7.3\lib\site-packages\pip\_vendor\cachecontrol\filewrapper.py", line 62, in read
data = self.__fp.read(amt)
File "d:\tan\soft\python3.7.3\lib\http\client.py", line 447, in read
n = self.readinto(b)
File "d:\tan\soft\python3.7.3\lib\http\client.py", line 491, in readinto
n = self.fp.readinto(b)
File "d:\tan\soft\python3.7.3\lib\socket.py", line 589, in readinto
return self._sock.recv_into(b)
File "d:\tan\soft\python3.7.3\lib\ssl.py", line 1052, in recv_into
return self.read(nbytes, buffer)
File "d:\tan\soft\python3.7.3\lib\ssl.py", line 911, in read
return self._sslobj.read(len, buffer)
socket.timeout: The read operation timed out
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "d:\tan\soft\python3.7.3\lib\site-packages\pip\_internal\cli\base_command.py", line 179, in main
status = self.run(options, args)
File "d:\tan\soft\python3.7.3\lib\site-packages\pip\_internal\commands\install.py", line 315, in run
resolver.resolve(requirement_set)
File "d:\tan\soft\python3.7.3\lib\site-packages\pip\_internal\resolve.py", line 131, in resolve
self._resolve_one(requirement_set, req)
File "d:\tan\soft\python3.7.3\lib\site-packages\pip\_internal\resolve.py", line 294, in _resolve_one
abstract_dist = self._get_abstract_dist_for(req_to_install)
File "d:\tan\soft\python3.7.3\lib\site-packages\pip\_internal\resolve.py", line 242, in _get_abstract_dist_for
self.require_hashes
File "d:\tan\soft\python3.7.3\lib\site-packages\pip\_internal\operations\prepare.py", line 334, in prepare_linked_requirement
progress_bar=self.progress_bar
File "d:\tan\soft\python3.7.3\lib\site-packages\pip\_internal\download.py", line 878, in unpack_url
progress_bar=progress_bar
File "d:\tan\soft\python3.7.3\lib\site-packages\pip\_internal\download.py", line 702, in unpack_http_url
progress_bar)
File "d:\tan\soft\python3.7.3\lib\site-packages\pip\_internal\download.py", line 946, in _download_http_url
_download_url(resp, link, content_file, hashes, progress_bar)
File "d:\tan\soft\python3.7.3\lib\site-packages\pip\_internal\download.py", line 639, in _download_url
hashes.check_against_chunks(downloaded_chunks)
File "d:\tan\soft\python3.7.3\lib\site-packages\pip\_internal\utils\hashes.py", line 62, in check_against_chunks
for chunk in chunks:
File "d:\tan\soft\python3.7.3\lib\site-packages\pip\_internal\download.py", line 607, in written_chunks
for chunk in chunks:
File "d:\tan\soft\python3.7.3\lib\site-packages\pip\_internal\utils\ui.py", line 159, in iter
for x in it:
File "d:\tan\soft\python3.7.3\lib\site-packages\pip\_internal\download.py", line 596, in resp_read
decode_content=False):
File "d:\tan\soft\python3.7.3\lib\site-packages\pip\_vendor\urllib3\response.py", line 494, in stream
data = self.read(amt=amt, decode_content=decode_content)
File "d:\tan\soft\python3.7.3\lib\site-packages\pip\_vendor\urllib3\response.py", line 459, in read
raise IncompleteRead(self._fp_bytes_read, self.length_remaining)
File "d:\tan\soft\python3.7.3\lib\contextlib.py", line 130, in __exit__
self.gen.throw(type, value, traceback)
File "d:\tan\soft\python3.7.3\lib\site-packages\pip\_vendor\urllib3\response.py", line 365, in _error_catcher
raise ReadTimeoutError(self._pool, None, 'Read timed out.')
pip._vendor.urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Read timed out.
办法一:太多
办法二:
pip install requests -i http://pypi.douban.com/simple --trusted-host=pypi.douban.com
Looking in indexes: http://pypi.douban.com/simple
Collecting requests
Downloading http://pypi.doubanio.com/packages/7d/e3/20f3d364d6c8e5d2353c72a67778eb189176f08e873c9900e10c0287b84b/requests-2.21.0-py2.py3-none-any.whl (57kB)
100% |████████████████████████████████| 61kB 15.4MB/s
Collecting chardet<3.1.0,>=3.0.2 (from requests)
Downloading http://pypi.doubanio.com/packages/bc/a9/01ffebfb562e4274b6487b4bb1ddec7ca55ec7510b22e4c51f14098443b8/chardet-3.0.4-py2.py3-none-any.whl (133kB)
100% |████████████████████████████████| 143kB 25.7MB/s
Collecting urllib3<1.25,>=1.21.1 (from requests)
Downloading http://pypi.doubanio.com/packages/62/00/ee1d7de624db8ba7090d1226aebefab96a2c71cd5cfa7629d6ad3f61b79e/urllib3-1.24.1-py2.py3-none-any.whl (118kB)
100% |████████████████████████████████| 122kB 25.7MB/s
Collecting certifi>=2017.4.17 (from requests)
Downloading http://pypi.doubanio.com/packages/60/75/f692a584e85b7eaba0e03827b3d51f45f571c2e793dd731e598828d380aa/certifi-2019.3.9-py2.py3-none-any.whl (158kB)
100% |████████████████████████████████| 163kB 25.7MB/s
Collecting idna<2.9,>=2.5 (from requests)
Downloading http://pypi.doubanio.com/packages/14/2c/cd551d81dbe15200be1cf41cd03869a46fe7226e7450af7a6545bfc474c9/idna-2.8-py2.py3-none-any.whl (58kB)
100% |████████████████████████████████| 61kB 15.4MB/s
Installing collected packages: chardet, urllib3, certifi, idna, requests
Successfully installed certifi-2019.3.9 chardet-3.0.4 idna-2.8 requests-2.21.0 urllib3-1.24.1
办法三:
到python社区找源码包https://pypi.org
下载后将该压缩包解压,然后打开cmd,并通过cmd进入到该安装包解压后的文件夹目录下
在该文件目录下输入命令:python setup.py install
因为我的requests模块在方法二已经安装过了,所以这里我截图显示的是beautifulsoup4模块的安装,只是名字不同,模块的安装都是一样的
然后运行from bs4 import BeautifulSoup时会出现以下错误:
You are trying to run the Python 2 version of Beautiful Soup under Python 3.
This will not work.'<>'You need to convert the code, either by installing it
(`python setup.py install`) or by running 2to3 (`2to3 -w bs4`)。
这时需要将BeautifulSoup4文件夹中的bs4文件夹拷贝到python安装目录下的lib中,并且将python安装目录下的Tools/scripts/2to3.py文件也剪切到python安装目录下的lib中。
最后在cmd中cd到lib目录,然后运行python 2to3.py bs4 -w即可。