京东商品信息爬虫
来源:互联网 发布:海阔天空 信乐团知乎 编辑:程序博客网 时间:2024/05/10 05:11
本文实现一个京东商品信息爬虫,通过python实现,可以交互式的输入要查询的商品,查询信息保存到csv文件中。
#京东商品信息爬虫#爬取京东商品信息并保存到csv格式文件中#2017-7-23import osimport requestsimport csvfrom bs4 import BeautifulSoup#获取url请求def gethtml(kind,page): '''获取url请求''' pagenum = str(2 * page) try: r = requests.get('https://search.jd.com/Search?keyword=' + \ kind + '&enc=utf-8&page=' + pagenum)#链接url r.raise_for_status() r.encoding = r.apparent_encoding#转码 print('爬取第{}页:'.format(page)) return r.text#返回html except: print('链接异常!!!') return ''#获取定位资源def findhtml(html,httplist): """寻找资源""" soup = BeautifulSoup(html,'lxml') links = soup.find_all('div', class_='gl-i-wrap')#寻找'div'标签 for link in links: ui = [] namediv = link.find('div', class_='p-name p-name-type-2')#寻找商品名称和链接 title = namediv.a['title'] href = namediv.a['href'] ui.append(title)#名称加入到ui中 pricediv = link.find('div', class_='p-price')#寻找商品价格 try: price = pricediv.strong['data-price'] ui.append(price)#价格加入到ui中 except: ui.append('') if 'https:' not in href:#添加链接 ui.append('https:' + href) else: ui.append(href) aggressmentdiv = link.find('div', class_='p-commit')#寻找评论 number = aggressmentdiv.strong.contents[1].string ui.append(number)#评论数添加到ui中 httplist.append(ui) try: if price: print('{:^10s}:{:<}元'.format(title,price)) else: print('{:^10s}'.format(title)) except: print('{:^10s}'.format(title))#保存资源def savehtml(ul): path = 'D:/数据/' if not os.path.exists(path): os.mkdir(path)#创建一个文件 with open(path + '京东商品信息爬虫.csv','w+') as f: writer = csv.writer(f) writer.writerow(['商品','价格','链接','评价数']) for u in range(len(ul)): if ul[u]: writer.writerow([ul[u][0],ul[u][1],ul[u][2],ul[u][3]])#程序主体if __name__ == '__main__': goods = input('请输入要搜索的物品:') yeshu = int(input('请输入要查询到的页数:')) ulist = [] for i in range(yeshu+1): try: if i != 0: text = gethtml(goods,i) findhtml(text,ulist) savehtml(ulist) except: break
阅读全文
1 0
- 京东商品信息爬虫
- 基于selenium和requests的京东商品信息和评论爬虫
- Java多线程爬虫爬取京东商品信息
- Python+Selenium+Chrome抓取京东商品信息
- 简单的商品信息爬虫——爬易迅网
- python爬虫实战(一)----------爬取京东商品信息
- 网站商品信息爬虫代码及详细注释
- 爬虫实践---Selenium-抓取淘宝搜索商品信息
- 商品信息
- CasperJS 抓取京东商品详情页中的商品信息
- 【Python】抓取京东列表页商品信息(selenium)
- 教您使用java爬虫gecco抓取JD全部商品信息
- 如何使用爬虫采集天猫店铺商品信息
- 基于Scrapy的爬虫爬取京东商品信息与评论
- <四>、python爬虫抓取购物网站商品信息--图片价格名称
- 如何使用淘宝商品信息采集爬虫-采集电商商品
- 教您使用java爬虫gecco抓取JD全部商品信息(一)
- 教您使用java爬虫gecco抓取JD全部商品信息(二)
- 使用php laravel进行数据数据采集
- Python 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等)
- hibernate懒加载的原理和作用
- WordPress傻瓜本地鸡安装&配置
- POJ
- 京东商品信息爬虫
- 系统销量统计
- text-shadow,box-shadow使用
- stack queue free-lock implate
- BFS/DFS
- <%@ taglib uri="" prefix=""%> 使用
- 了解log4j
- 域名来路判断
- List