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上执行?
发布于 2019-08-19 16:45:26
您应该打印异常消息
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发布于 2019-08-20 07:31:58
即使安装了bs4或beautifullsoup4,该问题也是由于缺少lxml解析器库造成的。安装方法: apt-get在安装bs4或beautifulsoup4之后安装python-lxml。
https://stackoverflow.com/questions/57552450
复制相似问题