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

时间:2019-08-19 07:46:43

标签: linux windows-10 python-3.6

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上执行?

2 个答案:

答案 0 :(得分:0)

您应该打印异常消息

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

答案 1 :(得分:0)

即使安装了bs4或beautifullsoup4,问题仍然是由缺少的lxml解析器库引起的。安装方法如下:在安装bs4或beautifulsoup4之后,apt-get install python-lxml。