Exemplo n.º 1
0
class PyqueryExtensionTest(TestCase):
    def setUp(self):
        # Create fake grab instance with fake response
        self.g = Grab(HTML, charset='cp1251')

    def test_some_things(self):
        from pyquery import PyQuery
        self.assertEqual(self.g.pyquery('#num-1').text(), u'item #100 2')
        self.assertEqual(self.g.pyquery('li').filter(
            lambda x: '#2' in PyQuery(this).text()).text(), u'item #2')
Exemplo n.º 2
0
class PyqueryExtensionTest(TestCase):
    def setUp(self):
        # Create fake grab instance with fake response
        self.g = Grab(HTML, charset='cp1251')

    def test_some_things(self):
        from pyquery import PyQuery
        self.assertEqual(self.g.pyquery('#num-1').text(), u'item #100 2')
        self.assertEqual(
            self.g.pyquery('li').filter(
                lambda x: '#2' in PyQuery(this).text()).text(), u'item #2')
Exemplo n.º 3
0
        def getproduct(href, item, scan, oneposition):
            pg = Grab(log_dir="/tmp",
                      timeout=30,
                      referer="http://hotline.ua" + href + 'prices/',
                      headers={'X-Requested-With': 'XMLHttpRequest'})
            purl = "http://hotline.ua" + href + '?tab=2&sort=0'
            pg.go(purl)

            f = open('/Users/a1/Desktop/myfile', 'w')
            f.write(pg.response.body)  # python will convert \n to os.linesep
            f.close(
            )  # you can omit in most cases as the destructor will call it

            # self.stdout.write(pg.response.body)

            if item.href != href:
                self.stdout.write('Href reloaded ' + item.name)
                item.href = href
                item.save()

            if pg.response.code == 200:
                # oneposition = OneHotline(product=item, scan=scan)
                # oneposition.save()

                i = 1
                tprice = 0
                positions = pg.pyquery(
                    '.new-price-line > div.tx-price.tx-price-line')

                # positions = pg.doc.select('//div[@id="tab2"]/div[@class="tx-price-line tx-price"]')
                # positions = pg.pyquery('#js-top-lines').remove()("div.tx-price-line")
                # positions = pg.pyquery('')
                for pos in positions:
                    onepos = pq(pos)
                    # assert False, onepos

                    maglink = onepos('.logo-box.fl a')[1].attrib['href']
                    self.stdout.write(maglink)
                    # assert False, maglink

                    mag = get_mag(maglink)

                    tprice = str(
                        onepos('a.orng')).split('>')[1].split('&#160;<i')[0]
                    if '&#160;' in tprice:
                        tempprice = tprice.split('&#160;')
                        tprice = tempprice[0] + tempprice[1]

                    if '.' in tprice:
                        tprice = tprice.split('.')[0]

                    tprice = int(tprice)

                    if i == 1:
                        oneposition.min = tprice

                    if mag.itemid == 22242:
                        oneposition.position = i
                    # else:
                    concrent = ConcurentHotline(one=oneposition,
                                                firm=mag,
                                                position=i,
                                                price=tprice)
                    concrent.save()

                    # assert False, maglink[0].attrib['href']
                    # assert False, str(onepos('a.orng')).split('>')[1].split('&#160;<i')[0]
                    # price = onepos('a.orng').split('>')[1].split('&')[0]
                    # assert False, tprice.decode('utf-8')

                    self.stdout.write('Product name: ' + item.name +
                                      ', Firm Link: ' + maglink + ', Price:' +
                                      str(tprice))

                    i += 1

                if tprice:
                    oneposition.max = tprice
                oneposition.total = i
                oneposition.save()

            waittime = random.randint(7, 10)
            self.stdout.write('Wait time:' + str(waittime))

            time.sleep(waittime)