def pop_item_url_from_id_list(a): if not a: return '' id = '' if py.isdict(a): id, v = a.popitem() if py.islist(a): id = a.pop() #-1 if py.islist(id): id = id[-3] if not id: return '' if 'id=' not in id: return 'https://item.taobao.com/item.htm?id=' + id else: return 'https://item.taobao.com/item.htm?id=' + T.sub(id, 'id=', '')
async def set_viewport(pages=py.No('auto all pages'), **ka): ''' #1347 看不到,1322 有留白''' if 'width' not in ka: ka['width'] = 1366 - 26 if 'height' not in ka: ka['height'] = 768 - 98 a = {'width': ka['width'], 'height': ka['height']} if not pages: pages = await taobao_trade.browser.pages() if not py.islist(pages): if not isinstance(pages, pyppeteer.page.Page): return py.No(U.v.isinstance(pages, pyppeteer.page.Page)) pages = [pages] for page in pages: await page.setViewport(a) return len(pages), ka
def load(s,skip_err_load=0): if py.istr(s): ls=[i.strip() for i in s.split(',')] if py.islist(s): ls=s print(ls) r=[] dlogin=U.get_or_set('lc.login',{}) for i in ls: if not i:continue f=r'C:/test/{}@qgbcs.uu.me.dill'.format(i) q=F.dill_load(f) if not q: print('#Err',f,repr(q)) if skip_err_load: continue else: raise q dlogin[i]=q r.append(q) return r
def result(shop=None): global grows if not shop: shop = gshop if py.islist(shop): items = shop shop = 'TB.result-test-{}'.format(len(items)) else: items = iter_items(shop) rows = py.set() for html in U.progressbar(items): bs = T.BeautifulSoup(html) a = bs.select('.item-name')[0] h = a.get('href') # '//item.taobao.com/item.htm?spm=a1z10.3-c.w4002-21992529001.30.1887510dqsrC5C&id=597761481418' if not h.startswith('//item.taobao.com/item.htm?') or 'id=' not in h: U.pln('not correct taobao item href', U.stime()) py.pdb() id = T.get_url_arg(h, 'id') cp = bs.select('[class=c-price]')[0] cp = float(cp.text) sp = bs.select('[class=s-price]') if sp: sp = sp[0] sp = float(sp.text) else: sp = cp img = bs.select('img')[0] row = [ int((sp - cp) * 100) / 100, cp, sp, id, T.replacey(a.text.strip(), [ '【优信电子】', ], ''), img ] row.insert(0, int((row[0] / row[1]) * 100)) row = tuple(row) rows.add(row) grows = rows return F.dill_dump(obj=rows, file='{}-{}'.format(shop, len(rows)))
async def iter_wuliu_detail(us=0): if not us: # sync_get_user=sync_get_user=U.get('sync_get_user') # try: # user=sync_get_user() # RuntimeError: This event loop is already running # except: # py.pdb() user = await get_user() assert user fs = [] for f in F.ls(U.gst, r=0): if 'trade_list' in f and user in f: fs.append(f) assert len(fs) > 0 us = F.dill_load(fs[-1]) page = await get_page() ls = F.ls(U.gst + __name__) for u in us: row = u if py.islist(u): u = u[-1] if not u: print(row) continue f = U.gst + __name__ + '/' + T.fileName(u) if f in ls: print('#skip', u) continue await page.goto(u) await asyncio.sleep(U.randomInt(9, 66)) # await asyncio.sleep(1) s = await page.content() f = F.write(f, s, mkdir=1) print(f, U.stime())
def select(iterable, **ka): ''' #<!-- # 不能用 editable="false" readyonly # <h6>包裹input无效 </h6> # background="green" 无效 , lavender淡紫色, 熏衣草花 ,#e6e6fa 太淡啦 # --> ''' response = U.get_duplicated_kargs(ka, 'p', 'resp', 'response', 'rp') request = U.get_duplicated_kargs(ka, 'q', 'req', 'request', 'requests', 'rq') url = U.get_duplicated_kargs(ka, 'url', 'mark_url', 'request_url') if N.is_flask_request(request) and not url: url = request.url if N.is_flask_request(url): url = request.url if url: url = T.url_decode(url) else: url = '' ha = ''' <head> <style type="text/css"> textarea { background: lightgoldenrodyellow; width:80%; # rows:1; # 无效 } </style> </head> <form action="/r=N.HTML.select_result(request,response)%23" method="post"> {rows} <input type="submit" > </form> ''' #################### hd = ''' <span > {i} </span> <input type="checkbox" name="{name}" {checked} > {k} </input> <textarea> {v} </textarea> <hr>''' ######################## def do_resp(r, kv, disabled): if response: rows = ''' <input type="text" readonly="readonly" name="{G_SELECT_ID}" value={id} style=" # background: aqua; background: cyan; " > <input type="text" readonly="readonly" name="{G_SELECT_URL}" value={url} style=" background: lightgray; width:80% "> <br> '''.format(url=url, id=id, G_SELECT_ID=G_SELECT_ID, G_SELECT_URL=G_SELECT_URL) i = 0 # fk=lambda k: T.html_encode(repr(k)) #为啥会出现 0 ☑ q." checked > ' fk = lambda _k: T.html_encode(repr(_k)) fv = lambda _v: T.html_encode(repr(_v)[:155 - 1]) # 全中文 80% 正好两行 # U.msgbox(list(kv)[:9]) for k, v in kv: # k,v=fk(k),fv(v) # U.msgbox(k,v) rows += hd.format(i=i, name=r.id(k, v), k=fk(k), v=fv(v), checked='checked') i += 1 for k, v in disabled: # k,v=fk(k),fv(v) rows += hd.format(i=i, name=r.id(k, v), k=fk(k), v=fv(v), checked='') i += 1 # py.importU().log(rows) response.headers['Content-Type'] = 'text/html;charset=utf-8' response.set_data(T.format(ha, rows=rows)) return r id = getattr(iterable, 'id', 0) if not id: id = py.id(iterable) if py.isdict(iterable): if isinstance(iterable, DictSelect): rd = iterable elif id not in gid_select: rd = gid_select[id] = DictSelect(iterable) rd.id = id else: rd = gid_select[id] return do_resp(rd, rd.items(), rd.disabled.items()) if py.islist(iterable): if isinstance(iterable, ListSelect): rd = iterable elif id not in gid_select: rd = gid_select[id] = ListSelect(iterable) rd.id = id else: rd = gid_select[id] return do_resp(rd, enumerate(rd), enumerate(rd.disabled))
async def set_cookies(ck, page=py.No('auto last -1')): if not page: page = await get_page() if py.islist(ck): await page.setCookie(*ck) if py.isdict(ck): await page.setCookie(ck)
async def del_cookies(ck, page=py.No('auto last -1')): page = await get_or_new_page(page) if py.islist(ck): await page.deleteCookie(*ck) if py.isdict(ck): await page.deleteCookie(ck)