python 引入 requests 报错问题

#!/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


中间虽然有报错的。但是任然成功安装了。至此问题解决。


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值