当前位置: 首页 > news >正文

vscode中安装python运行调试环境

在运行代码之前,需要到微软商店下载安装python环境,35m,都是自动的。

1、安装python 的extensions插件。

ctrl+shift+x 输入 python 后点击 install 按钮。

2、新建文件夹spider文件夹。 

3、在新建文件夹spider下新建文件spider.py源代码。

4、遇到问题,升级pip执行文件。

pip install request

报如下错误:

WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError(SSLEOFError(8, 'EOF occurred in violation of protocol (_ssl.c:997)'))': /simple/request/
WARNING: Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError(SSLEOFError(8, 'EOF occurred in violation of protocol (_ssl.c:997)'))': /simple/request/
ERROR: Could not find a version that satisfies the requirement request (from versions: none)
ERROR: No matching distribution found for request

解决: 

C:\Users\zhang\AppData\Local\Microsoft\WindowsApps\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\python.exe -m pip install --upgrade pip

5、遇到问题,pip命令行修改。

再次

pip install request

报如下错误:

ERROR: Could not find a version that satisfies the requirement request (from versions: none)
ERROR: No matching distribution found for request

解决:

转换下载地址:douban

pip install request-i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com

接着报错:

ERROR: Cannot unpack file C:\Users\zhang\AppData\Local\Temp\pip-unpack-2ejahzt3\simple.html (downloaded from C:\Users\zhang\AppData\Local\Temp\pip-req-build-xfd621gj, content-type: text/html); cannot detect archive format
ERROR: Cannot determine archive format of C:\Users\zhang\AppData\Local\Temp\pip-req-build-xfd621gj

解决:换成清华的网址,改写requests【加s】

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple --trusted-host pypi.tuna.tsinghua.edu.cn requests

按照以上方法,分别安装bs4,openpyxl模块

6、按运行按钮执行,或者在控制台程序中输入命令:

python spider.py

最后发现一切的原因来自于requests 少打了一个s字母。

以下是chatgpt写的爬虫代码:

import requests
from bs4 import BeautifulSoup
import openpyxl# Make the request and parse the HTML content
url = "https://books.toscrape.com/"
response = requests.get(url)
soup = BeautifulSoup(response.text, "html.parser")# Extract the book data
books = []
for article in soup.find_all("article"):book = {}book["title"] = article.h3.a["title"]book["price"] = article.select_one(".price_color").get_text(strip=True)books.append(book)# Save the book data to an Excel file
wb = openpyxl.Workbook()
sheet = wb.active
sheet.append(["Title", "Price"])
for book in books:sheet.append([book["title"], book["price"]])
wb.save("books.xlsx")

网址:All products | Books to Scrape - Sandbox

结果:

 

http://www.lryc.cn/news/1362.html

相关文章:

  • 【微服务】微服务架构超强讲解,通俗易懂
  • 内核中的竞态产生的原因和解决方法
  • 【微服务】Elasticsearch文档索引库操作(二)
  • 【论文速递】NAACL2022-DEGREE: 一种基于生成的数据高效事件抽取模型
  • C++类和对象(下)
  • Java常见的六种线程池、线程池-四种拒绝策略总结
  • Node=>Express中间件分类 学习4
  • 在阿里当外包,是一种什么工作体验?
  • Vue3快速入门【二】
  • C++-类和对象(上)
  • CAPL(vTESTStudio) - DoIP - TCP接收_04
  • 联合培养博士经历对于国内就业有优势吗?
  • 测试左移之需求质量
  • 【数据结构初阶】第三节.顺序表详讲
  • 新手小白适合做跨境电商吗?
  • Python搭建自己[IP代理池]
  • pandas——plot()方法可视化
  • 【Three.js基础】坐标轴辅助器、requestAnimationFrame处理动画、Clock时钟、resize页面尺寸(二)
  • C++之完美转发、移动语义(forward、move函数)
  • LeetCode刷题系列 -- 48. 旋转图像
  • 在多线程环境下使用哈希表
  • 【排序算法】堆排序(Heap Sort)
  • 分类预测 | Matlab实现SSA-RF和RF麻雀算法优化随机森林和随机森林多特征分类预测
  • Allegro如何添加ICT操作指导
  • 软件架构设计(二)——领域架构、基于架构的软件开发方法
  • 数组常用方法(2)---数组遍历方法
  • 卸载Node.js
  • 发表计算机SCI论文,会经历哪些过程? - 易智编译EaseEditing
  • python中lambda的用法
  • 网络安全协议(3)