Simple script to return # of eBay search results for a specified keyword in Python 2.
I'm new to coding so please feel free to improve/fix errors. :bowdown:Code:#!/usr/bin/python import urllib2 import re searchkey = "your keyword here" stripkey = searchkey.replace(" ","+") request = urllib2.Request('http://www.ebay.com/sch/i.html?_nkw=' + stripkey) request.add_header('UserAgent', 'Mozilla Firefox') response = urllib2.urlopen(request) for line in response.read().split('\n'): match = re.search("untClass'>[0-9]+(,[0-9]+)*", line, re.I) if match: print match.group().strip("untClass'>")
P.S. My goal is to integrate this with Sikuli to build an awesome automated jython-powered scraping/analyzing setup. :rasta:
Good first forum post When you're inserting a variable into a url, it's best to use urllib.quote_plus on the variable. For the regex, not sure why you're looping through each line, re.S might be helpful to you though.
XPaths are great for this sort of thing, more resiliant to code slightly changing, as well as quicker to write once you're proficient.
Here's my one liner:
Code:
import web
import urllib
keyword = 'potato peeler'
print web.grab('http://www.ebay.com/sch/i.html?_nkw=%s' % urllib.quote_plus(keyword)).single_xpath('//span[@class="countClass"]/text()').replace(',','')
That still uses urllib2 under the hood. To get xpath with any random html (such as from sikuli):
Code:
from lxml import etree
doc = etree.HTML(htmldata)
print doc.xpath('//somexpath')