vscode中安装python运行调试环境

编程知识 行业动态 更新时间:2024-06-13 00:20:19

在运行代码之前,需要到微软商店下载安装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/simple/ --trusted-host pypi.douban

接着报错:

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/simple --trusted-host pypi.tuna.tsinghua.edu 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/"
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

结果:

 

更多推荐

vscode中安装python运行调试环境

本文发布于:2023-03-29 17:22:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/1d334658ff8cc7e43f8461b2591569d5.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:环境   vscode   python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!