#!/usr/bin/env python # coding:utf-8 import requests url = 'http://www.baidu.com' data = requests.get(url) print(data) print(data.text) # 这里.text等同于read() code = data.encoding print(code) page_status = data.status_code print(page_status)
今天在公司客户提出要抓取大量供应商信息。手动搜索太慢就想着能不能搞个爬虫玩玩。没想到还没上路就遇到一个坑硬是折腾了两个小时。在此记录一下
问题:引入requests包(暂且这么称呼吧)时一直有红色波浪线 找不到源。
猜测1:虚拟环境没有创建好。
New python executable in D:\xinyuan\cs001\venv\Scripts\python.exe
Installing setuptools, pip, wheel...
Complete output from command D:\xinyuan\cs001\venv\Scripts\python.exe - setuptools pip wheel:
Collecting setuptools
Downloading https://files.pythonhosted.org/packages/20/d7/04a0b689d3035143e2ff288f4b9ee4bf6ed80585cc121c90bfd85a1a8c2e/setuptools-39.0.1-py2.py3-none-any.whl (569kB)
Exception:
下载 setuptools-39.0.1-py2.py3-none-any.whl 超时引起。
修改办法:在 C:\Users\Administrator\AppData\Roaming\pip 路径下创建 pip.ini文件。
文件内容 :[global]
timeout = 60000
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
[install]
use-mirrors = true
mirrors = https://pypi.tuna.tsinghua.edu.cn
修改超时时间及资源来源为国内来源。
虚拟环境创建问题解决,导入问题未解决。
猜测2:没有安装requests包。执行命令 pip uninstall requests.
提示
果然没有安装。安装在别的路径也会引起该问题。
执行命令 pip install requests
中间虽然有报错的。但是任然成功安装了。至此问题解决。