黑松山资源网 Design By www.paidiu.com
本文介绍了Python数据抓取分析,分享给大家,具体如下:
编程模块:requests,lxml,pymongo,time,BeautifulSoup
首先获取所有产品的分类网址:
def step(): try: headers = { 。。。。。 } r = requests.get(url,headers,timeout=30) html = r.content soup = BeautifulSoup(html,"lxml") url = soup.find_all(正则表达式) for i in url: url2 = i.find_all('a') for j in url2: step1url =url + j['href'] print step1url step2(step1url) except Exception,e: print e
我们在产品分类的同时需要确定我们所访问的地址是产品还是又一个分类的产品地址(所以需要判断我们访问的地址是否含有if判断标志):
def step2(step1url): try: headers = { 。。。。 } r = requests.get(step1url,headers,timeout=30) html = r.content soup = BeautifulSoup(html,"lxml") a = soup.find('div',id='divTbl') if a: url = soup.find_all('td',class_='S-ITabs') for i in url: classifyurl = i.find_all('a') for j in classifyurl: step2url = url + j['href'] #print step2url step3(step2url) else: postdata(step1url)
当我们if判断后为真则将第二页的分类网址获取到(第一个步骤),否则执行postdata函数,将网页产品地址抓取!
def producturl(url): try: p1url = doc.xpath(正则表达式) for i in xrange(1,len(p1url) + 1): p2url = doc.xpath(正则表达式) if len(p2url) > 0: producturl = url + p2url[0].get('href') count = db[table].find({'url':producturl}).count() if count <= 0: sn = getNewsn() db[table].insert({"sn":sn,"url":producturl}) print str(sn) + 'inserted successfully' else: 'url exist' except Exception,e: print e
其中为我们所获取到的产品地址并存入mongodb中,sn作为地址的新id。
下面我们需要在mongodb中通过新id索引来获取我们的网址并进行访问,对产品进行数据分析并抓取,将数据更新进数据库内!
其中用到最多的BeautifulSoup这个模块,但是对于存在于js的价值数据使用BeautifulSoup就用起来很吃力,所以对于js中的数据我推荐使用xpath,但是解析网页就需要用到HTML.document_fromstring(url)方法来解析网页。
对于xpath抓取价值数据的同时一定要细心!如果想了解xpath就在下面留言,我会尽快回答!
def parser(sn,url): try: headers = { 。。。。。。 } r = requests.get(url, headers=headers,timeout=30) html = r.content soup = BeautifulSoup(html,"lxml") dt = {} #partno a = soup.find("meta",itemprop="mpn") if a: dt['partno'] = a['content'] #manufacturer b = soup.find("meta",itemprop="manufacturer") if b: dt['manufacturer'] = b['content'] #description c = soup.find("span",itemprop="description") if c: dt['description'] = c.get_text().strip() #price price = soup.find("table",class_="table table-condensed occalc_pa_table") if price: cost = {} for i in price.find_all('tr'): if len(i) > 1: td = i.find_all('td') key=td[0].get_text().strip().replace(',','') val=td[1].get_text().replace(u'\u20ac','').strip() if key and val: cost[key] = val if cost: dt['cost'] = cost dt['currency'] = 'EUR' #quantity d = soup.find("input",id="ItemQuantity") if d: dt['quantity'] = d['value'] #specs e = soup.find("div",class_="row parameter-container") if e: key1 = [] val1= [] for k in e.find_all('dt'): key = k.get_text().strip().strip('.') if key: key1.append(key) for i in e.find_all('dd'): val = i.get_text().strip() if val: val1.append(val) specs = dict(zip(key1,val1)) if specs: dt['specs'] = specs print dt if dt: db[table].update({'sn':sn},{'$set':dt}) print str(sn) + ' insert successfully' time.sleep(3) else: error(str(sn) + '\t' + url) except Exception,e: error(str(sn) + '\t' + url) print "Don't data!"
最后全部程序运行,将价值数据分析处理并存入数据库中!
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
黑松山资源网 Design By www.paidiu.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
黑松山资源网 Design By www.paidiu.com
暂无评论...
更新日志
2024年10月09日
2024年10月09日
- 【原神】V5.0攻略 | 林尼攻略一图流
- 李翊君.1993-相思的烈酒【上华】【WAV+CUE】
- 古巨基.1998-LEO.KU(国)【千禧年代】【WAV+CUE】
- 郭子.2001-原来你什么都不想要创作集丫滚石】【WAV+CUE】
- 《使命召唤:黑色行动6》新预告公布!10月25日发售
- Atlus《暗喻幻想》媒体评分汇总:高分好评如潮!
- 2024金摇杆奖提名揭晓 《黑神话》角逐最佳视觉设计!
- 群星《新说唱2024 第3期 (上)》[320K/MP3][32.76MB]
- 群星《新说唱2024 第3期 (上)》[FLAC/分轨][95.38MB]
- 群星《新说唱2024 第3期 (下)》[320K/MP3][31.36MB]
- 幻兽帕鲁手游什么时候正式上线 最新消息一览
- 西普大陆BOSS位置盘点 解锁天启纪元玩法
- 西普大陆精灵进阶培养攻略 精灵养成指南
- dnf手游法控法系职业哪个强 dnf手游法控法系职业强度排行
- 魔兽世界血藤护目镜图纸在哪买 wlk血藤护目镜图纸购买位置介绍