My code scrapes data reliably on my machine, but not others
<p>I use Ubuntu 16.04, and I created a code that takes in an eBay link and scrapes the amount of units of that product sold, if the information is available. My version looks like this:</p>

<pre><code>import requests
from bs4 import BeautifulSoup

def sold(url):
  soup = BeautifulSoup(requests.get(url).text, 'html.parser')
  amount_sold = soup.find('span', attrs={'class':"qtyTxt vi-bboxrev-dsplblk  vi-qty-fixAlignment"})
  if amount_sold:
    amount_sold = amount_sold.find('a')
    amount_sold = amount_sold.get_text().replace(u',', u'')[:-5]
  else:
    amount_sold = "N/A"
</code></pre>

<p>When I use this function on my machine, it reliably scrapes the data every time it is available. For example, for this page: <a href="http://www.ebay.com/itm/13-lighted-led-bat-halloween-window-silhouette-decoration/311651989599?hash=item488fe7f85f" rel="nofollow">http://www.ebay.com/itm/13-lighted-led-bat-halloween-window-silhouette-decoration/311651989599?hash=item488fe7f85f</a> , it will reliably return 53 (at the time of this writing). However, when I try this code on another machine (on the other machine I've tried Windows, Cygwin, and even an external Ubuntu server), my function returns "N/A", because my amount_sold variable does not find anything at all. I went into the page to make sure that the html code is the same, and when I tested my code by manually writing out each line on this other machine, it worked perfectly. It is only when it runs within a function that it fails to work.</p>

<p>Is there a way to ensure that my code will work on any machine? What are some reasons the code may work on my own, but not on a different machine? And why does the code work when I test it line-by-line, but not when it's inside a function?</p>

<p>I don't know how you got it to work on your machine but to search for a tag with multiple classes, you're supposed to pass in as a list whose elements are the names of the classes you're interested in. like this:</p>

<pre><code>soup.find('span', attrs={'class':["qtyTxt", "vi-bboxrev-dsplblk", "vi-qty-fixAlignment"]})
</code></pre>

<p>I suspect you're using a different version of <code>beautifulsoup</code> that allows you to pass in the classes as a single string (i don't know of any version that does that).</p>

<p>This answer was tested on <code>beautifulsoup4</code> version <code>4.5</code></p>

<p>This is the output from <code>pip freeze</code> on my machine.</p>

<p><code>beautifulsoup4==4.5.0</code></p>

python web-scraping ebay