首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么Python代码可以在Windows 10中运行,而Linux不能?

为什么Python代码可以在Windows 10中运行,而Linux不能?
EN

Stack Overflow用户
提问于 2019-08-19 15:46:43
回答 2查看 81关注 0票数 0

python脚本可以在Windows 10上按预期执行,但在Linux上不能。

代码语言:javascript
复制
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上执行?

EN

回答 2

Stack Overflow用户

发布于 2019-08-19 16:45:26

您应该打印异常消息

代码语言:javascript
复制
except Exception as e:
    print(e)

在第一次执行时,请求失败,并显示以下消息

代码语言:javascript
复制
Couldn't find a tree builder with the features you requested: lxml. Do you need to install a parser library?

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

结果

代码语言:javascript
复制
 Product at row, costs = 62.82 
756.31

额外信息

在安装bs4的过程中出现了一些错误信息,我不知道它是否与执行错误有关

代码语言:javascript
复制
% 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
票数 0
EN

Stack Overflow用户

发布于 2019-08-20 07:31:58

即使安装了bs4或beautifullsoup4,该问题也是由于缺少lxml解析器库造成的。安装方法: apt-get在安装bs4或beautifulsoup4之后安装python-lxml。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57552450

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档