gpt4 book ai didi

linux - 为什么 Python 代码可以在 Windows 10 而不是 Linux 中运行?

转载 作者:太空宇宙 更新时间:2023-11-04 11:46:24 25 4
gpt4 key购买 nike

python 脚本在 Windows 10 上按预期执行,但在 Linux 上却没有。

import requests    
from bs4 import BeautifulSoup

urlCalculator = 'https://salecalc.com/ebay?t=1&cp=12&b=&sp=&s=&r=&q=1&ct=45&sc=&mc=&pt=2&g=15&c=11&fi=on&st=0&pl=1&pe=2.9&pf=0.30&m=1&o=0'

try:
source = requests.get(urlCalculator).text
soup = BeautifulSoup(source,'lxml')
targetPrice = soup.find(class_="target-value").text
listingPrice = targetPrice[1:]
print(" Product at row, costs = %s " % (listingPrice))
except:
print('request failed')

url = 'https://salecalc.com/ebay?t=1&cp=12&b=&sp=&s=&r=&q=1&ct=545&sc=&mc=&pt=2&g=15&c=11&fi=on&st=0&pl=1&pe=2.9&pf=0.30&m=1&o=0'

try:
sr = requests.get(url)
sp = BeautifulSoup(sr.content, 'lxml')
target = sp.find(class_="target-value").text
listingP = target[1:]
print(listingP)
except:
print('another failure')

为什么脚本在Linux上执行失败?

最佳答案

你应该打印异常信息

except Exception as e:
print(e)

第一次执行请求失败并显示消息

Couldn't find a tree builder with the features you requested: lxml. Do you need to install a parser library?

搜索错误消息建议我安装 lxml

结果

 Product at row, costs = 62.82 
756.31

额外信息

安装bs4时出现了一些错误提示,不知道是不是执行错误导致的

% pip3 install bs4     
# at 10:35:07
Collecting bs4
Downloading https://files.pythonhosted.org/packages/10/ed/7e8b97591f6f456174139ec089c769f89a94a1a4025fe967691de971f314/bs4-0.0.1.tar.gz
Collecting beautifulsoup4 (from bs4)
Downloading https://files.pythonhosted.org/packages/1a/b7/34eec2fe5a49718944e215fde81288eec1fa04638aa3fb57c1c6cd0f98c3/beautifulsoup4-4.8.0-py3-none-any.whl (97kB)
100% |████████████████████████████████| 102kB 611kB/s
Collecting soupsieve>=1.2 (from beautifulsoup4->bs4)
Downloading https://files.pythonhosted.org/packages/0b/44/0474f2207fdd601bb25787671c81076333d2c80e6f97e92790f8887cf682/soupsieve-1.9.3-py2.py3-none-any.whl
Building wheels for collected packages: bs4
Running setup.py bdist_wheel for bs4 ... error
Complete output from command /home/alex/tmp/python/beautifulsoup/bin/python3 -u -c "import setuptools, tokenize;__file__='/tmp/pip-install-25dqrm0m/bs4/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" bdist_wheel -d /tmp/pip-wheel-awj6spxb --python-tag cp37:
usage: -c [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...]
or: -c --help [cmd1 cmd2 ...]
or: -c --help-commands
or: -c cmd --help

error: invalid command 'bdist_wheel'

----------------------------------------
Failed building wheel for bs4
Running setup.py clean for bs4
Failed to build bs4
Installing collected packages: soupsieve, beautifulsoup4, bs4
Running setup.py install for bs4 ... done
Successfully installed beautifulsoup4-4.8.0 bs4-0.0.1 soupsieve-1.9.3

关于linux - 为什么 Python 代码可以在 Windows 10 而不是 Linux 中运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57552450/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com