0

The python script executes as expected on Windows 10, but not on 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')

Why does the script fail to execute on Linux?

jww
  • 97,681
  • 90
  • 411
  • 885

2 Answers2

1

The problem was caused by a missing lxml parser library even though bs4 or beautifullsoup4 was installed. Install it by: apt-get install python-lxml after installation of bs4 or beautifulsoup4.

0

You should print the exception message

except Exception as e:
    print(e)

At the first execution the request failed with the message

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

A search for the error message suggested me to install lxml

Result

 Product at row, costs = 62.82 
756.31

extra info

There was some error message during the installation of bs4, I don't know if it can be associated with the execution error

% 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
Alex
  • 3,264
  • 1
  • 25
  • 40
  • Thanks for the pointer. It helped resolve my issue. Just wondering as to why the lxml parser library was missing when i had installed bs4 or beautifulsoup4. – Sika Newman Aug 19 '19 at 23:22
  • @SikaNewman have you seen this question? [Do you need to install a parser library?](https://stackoverflow.com/q/24398302/2498790) – Alex Aug 20 '19 at 05:10
  • No, i haven't seen that question since my issue has already been resolved. The lxml parser library was missing for some reason. On windows i didn't encounter such a problem, only on linux based os. – Sika Newman Aug 21 '19 at 06:12