ios中的爬虫

爬虫说白了就是通过程序从网页上获取目标信息

网络爬虫(又被称为网页蜘蛛,网络机器bai人,在FOAF社区中间,更经常被称为网页追逐者),是一种按照一定的规则,自动的抓取万维网信息的程序或者脚本,已被广泛应用于互联网领域。

网络爬虫另外一些不常使用的名字还有蚂蚁,自动索引,模拟程序或者蠕虫。

 

比如你知道了某个接口就可以直接请求响应的信息  无论返回的是json 还是 网页 都可以解析出你要的信息

比如某电商平台的某个商品的实时价格监听爬取 只要你知道这个地址 和商品id 就可以了

http://p.3.cn/prices/mgets?skuIds=J_2510388

 

其实爬虫比较专业的语言是Python

如下就是一段Python的 爬虫代码 

import requests from bs4 import BeautifulSoup import xlwt class Excel: # 当前行数 _current_row = 1 # 初始化,创建文件及写入title def __init__(self, sheet_name='sheet1'): # 表头,放到数组中 title_label = ['商品编号', '商品名称', '图片路径', '价格', '商家', '商品详情地址'] self.write_work = xlwt.Workbook(encoding='ascii') self.write_sheet = self.write_work.add_sheet(sheet_name) for item in range(len(title_label)): self.write_sheet.write(0, item, label=title_label[item]) # 写入内容 def write_content(self, content): for item in range(len(content)): self.write_sheet.write(self._current_row, item, label=content[item]) # 插入完一条记录后,换行 self._current_row += 1 # 保存文件(这里的'./dj_data.xls'是默认路径,如果调用此函数,没有传file_url参数,则使用'./dj_data.xls') def save_file(self, file_url='./dj_data.xls'): try: self.write_work.save(file_url) print("文件保存成功!文件路径为:" + file_url) except IOError: print("文件保存失败!") def get_html(url): # 模拟浏览器访问 headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) ' 'AppleWebKit/537.36 (KHTML, like Gecko) ' 'Chrome/81.0.4044.138 Safari/537.36', 'accept-language': 'zh-CN,zh;q=0.9' } print("--> 正在获取网站信息") response = requests.get(url, headers=headers) # 请求访问网站 if response.status_code == 200: html = response.text # 获取网页源码 return html # 返回网页源码 else: print("获取网站信息失败!") if __name__ == '__main__': # 创建文件 excel = Excel() # 搜索关键字 keyword = 'aj1' # 搜索地址 search_url = 'https://search.jd.com/Search?keyword=' + keyword + '&enc=utf-8' html = get_html(search_url) # 初始化BeautifulSoup库,并设置解析器 soup = BeautifulSoup(html, 'lxml') # 商品列表 goods_list = soup.find_all('li', class_='gl-item') # 打印goods_list到控制台 for li in goods_list: # 遍历父节点 # 商品编号 no = li['data-sku'] # 商品名称 name = li.find(class_='p-name p-name-type-2').find('em').get_text() # 图片路径 img_url = li.find(class_='p-img').find('img')['src'] # 价格 price = li.find(class_='p-price').find('i').get_text() # 商家 shop = li.find(class_='p-shop').find('a').get_text() # 商品详情地址 detail_addr = li.find(class_='p-name p-name-type-2').find('a')['href'] # 将商品信息放入数组中,再传到写入文件函数 goods = [no, name, img_url, price, shop, detail_addr] # 写入文档 excel.write_content(goods) # 保存文件,使用的是相对目录(也可以使用绝对路径),会保存在当前文件的同目录下。文件名为dj_data.xls,必须是.xls后缀 excel.write_work.save("C:/Users/86135/Desktop/dj_data.xls")

 

 

 

参考链接:

https://www.cnblogs.com/byuc/p/7190122.html

https://baike.baidu.com/item/网络爬虫/5162711?fromtitle=爬虫&fromid=22046949&fr=aladdin

https://www.jianshu.com/p/aa3fc03471f6

https://blog.csdn.net/guoxuying/article/details/108183249?utm_medium=distribute.pc_aggpage_search_result.none-task-blog-2~all~top_click~default-2-108183249.nonecase&utm_term=京东爬虫&spm=1000.2123.3001.4430

 

posted on 2016-05-03 10:04  🌞Bob  阅读(464)  评论(0编辑  收藏  举报

导航