import urllib, base64 from bs4 import BeautifulSoup
def fetch(k):
url = base64.decodestring("aHR0cDovL3NlYXJjaC5qZC5jb20vU2VhcmNoP2tleXdvcmQ9JXMmZW5jPXV0Zi04JndxPSVz") % ((urllib.quote(k),)*2)
ret = map(lambda l:[l.attrs.get('data-sku') or '']+map(lambda x: x.text.strip() if x else '',[l.select_one(css) for css in ['div.p-price i', 'div.p-name a']])+(lambda x: [x.attrs.get('src') or x.attrs.get('data-lazy-img')] if x else [''])(l.select_one('div.p-img a img')), BeautifulSoup
(urllib.urlopen(url), 'html.parser', from_encoding='utf-8').select("#J_goodsList ul li"))
return filter(lambda r: True if '' not in r else False, ret)
if name == "main":
for l in fetch('iphone'):
print l
1
Kilerd 2016-11-06 13:42:52 +08:00
base64.decodestring("aHR0cDovL3NlYXJjaC5qZC5jb20vU2VhcmNoP2tleXdvcmQ9JXMmZW5jPXV0Zi04JndxPSVz")
不懂你为什么要先 encode 再 decode 。隐藏什么东西??? |
2
Kilerd 2016-11-06 13:43:15 +08:00
aHR0cDovL3NlYXJjaC5qZC5jb20vU2VhcmNoP2tleXdvcmQ9JXMmZW5jPXV0Zi04JndxPSVz
http://search.jd.com/Search?keyword=%s&enc=utf-8&wq=%s |
3
purebluesong 2016-11-06 13:46:09 +08:00
握草, 新式广告?
|
4
veelog 2016-11-06 14:00:12 +08:00 via Android
乱了,复制下来运行了下,是爬 jd 商品价格的爬虫
|
5
est 2016-11-06 14:07:37 +08:00
现在爬价格还不够啊。还得把各种券的组合算出来。。。累。
|
6
msg7086 2016-11-06 15:31:07 +08:00
玩 one-liner 为什么不去玩 ruby ……
|
7
yuankui 2016-11-06 20:44:39 +08:00
楼主你要牛逼啊,居然能写出这么难看的代码!佩服
|
8
aihimmel 2016-11-06 21:39:51 +08:00 via Android
过于炫技了吧, lambda 满天飞
|
9
aihimmel 2016-11-06 21:42:01 +08:00 via Android
PEP8 标准是一行少于 79 字符,不如拆开写。
|
10
jackyspy 2016-11-07 10:19:01 +08:00
就算炫技,也要写的别人能看清楚,对不?
``` def fetch(k): url = 'http://search.jd.com/Search?keyword={k}&enc=utf-8&wq={k}'.format( k=urllib.quote(k)) return filter(all, ( [l.attrs.get('data-sku')] + [x and x.text.strip() for x in map(l.select_one, [ 'div.p-price i', 'div.p-name a'])] + (lambda x: [x and (x.attrs.get('src') or x.attrs.get('data-lazy-img'))])( l.select_one('div.p-img a img')) for l in BeautifulSoup(urllib.urlopen(url), 'html.parser', from_encoding='utf-8' ).select("#J_goodsList ul li") )) ``` |
11
justyy 2016-11-07 18:39:03 +08:00
程序要让你看得懂,看不懂 的程序 任何语言都可以写出来。
PYTHON 的优点是写出来的程序 简洁清楚,而不是这样玩。 |