使用python利器selenium工具模拟浏览器运行并爬取淘宝商品信息
来源:互联网 发布:手机网络渗透 编辑:程序博客网 时间:2024/05/01 17:30
#导入获取浏览器驱动的模块from selenium import webdriver#用于获取网页元素from selenium.webdriver.common.by import By#webdriver.wait库负责循环等待网页加载的时间from selenium.webdriver.support.ui import WebDriverWait#expected_conditions类负责等待网页加载的条件from selenium.webdriver.support import expected_conditions as Ec#导入超时的模块from selenium.common.exceptions import TimeoutException#分析网页from bs4 import BeautifulSoup#使用nosql数据库mongodb存取数据import pymongo#获得谷歌浏览器的网站驱动driver=webdriver.Chrome()wait=WebDriverWait(driver,10)list=[]#商品搜索def search(shop=None): print('开始搜索商品....') driver.get('http://www.taobao.com') try: #找到淘宝主页中的搜索框 input=wait.until( Ec.presence_of_element_located((By.CSS_SELECTOR,'#q')) ) #向搜索框中放入要查找的商品名称 input.send_keys(shop) #找到淘宝主页中的搜索提交按钮 submit=wait.until( Ec.element_to_be_clickable((By.CSS_SELECTOR,'div[class="search-button"]>button[class="btn-search tb-bg"]' )) ) #点击按钮提交搜索 submit.click() #调用下一个函数采集商品详细信息 get_response() #如果超时,重新查找 except TimeoutException: return search(shop)#提取商品信息def get_response(): print('已经找到该商品') # wait.until( # # Ec.presence_of_all_elements_located((By.CSS_SELECTOR,'#mainsrp-itemlist .items .item')) # ) #获取跳转成功后的网页源代码 html=driver.page_source #用bs4解析网页 soup=BeautifulSoup(html,'lxml') #获取商品信息所在的div items=soup.find('div',class_='m-itemlist').find_all('div',class_='item') #遍历所有商品,获取详细信息 for item in items: product={ 'image':item.find('a').find('img')['src'].strip(), 'price':item.find('div',class_='price g_price g_price-highlight' ).text.strip(), 'number':item.find('div',class_='deal-cnt').text[:-3].strip(), 'title':item.find('div',class_='row row-2 title').text.strip(), 'location':item.find('div',class_='location').text.strip() } print(product) list.append(product)#分页功能def next_page(page): print('当前是第{}'.format(page),'页') try: #定位到输入页数的输入框 input=wait.until( Ec.presence_of_element_located((By.CSS_SELECTOR,'#mainsrp-pager > div > div > div > div.form > input')) ) #定位到输入页数后的确定按钮 submit=wait.until( Ec.element_to_be_clickable((By.CSS_SELECTOR,'#mainsrp-pager > div > div > div > div.form > span.btn.J_Submit')) ) input.clear()#清除输入框中到的内容 input.send_keys(page)#输入框中放入内容 submit.click() wait.until( #判断某个元素中的text是否包含了预期的字符串,也就是页数 Ec.text_to_be_present_in_element((By.CSS_SELECTOR,'#mainsrp-pager > div > div > div > ul > li.item.active > span'),str(page)) ) get_response() except TimeoutException: return next_page(page)# 存入到mongodb数据库中def save_into_mongo(list): #创建数据库连接 client=pymongo.MongoClient('localhost') #创建数据库 db=client['taobao'] #创建数据表 content=db['info'] #数据插入到数据表中 content.insert(list)if __name__=='__main__': shop=input('输入名字:') search(shop) for i in range(2,5): next_page(i) # save_into_mongo(list)
阅读全文
0 0
- 使用python利器selenium工具模拟浏览器运行并爬取淘宝商品信息
- 使用python + selenium爬取淘宝商品信息
- python +selenium 爬取淘宝网商品信息
- selenium和pyquery爬取淘宝美食商品信息
- python 使用selenium+urllib爬取淘宝MM照片
- Python使用Selenium爬取淘宝异步加载的数据
- Python爬取淘宝搜索页,使用Selenium+BeautifulSoup
- Python爬虫-Selenium爬取淘宝美食
- python selenium 爬取淘宝商品数据
- 使用Selenium模拟浏览器抓取淘宝商品美食信息
- 使用python+selenium爬取学生信息并入库
- selenium、python模拟登陆淘宝
- 使用requests和re库对淘宝商品信息进行定向爬取
- python 登录并爬取淘宝信息
- Python实战1_2:爬取商品信息
- 比价网站的基础-爬取淘宝的商品信息
- requests模块实践:爬取淘宝商品信息和价格
- Selenium+PhantomJS爬取淘宝
- ImageView(图像视图)-Java代码中设置blackground和src属性:
- win10启动u盘制作教程
- 会说话的PPT,从开发者角度十分钟理解区块链
- LeetCode算法问题4 —— Longest Palindromic Substring
- Android Studio导入第三方库
- 使用python利器selenium工具模拟浏览器运行并爬取淘宝商品信息
- 2017_9_9
- ContentProvider 内容提供器
- 最长公共子序列
- 1019. General Palindromic Number (20)
- 二分查找应用-旋转数组
- Rails 十日谈
- Hibernate笔记
- Kotlin 第九章:可见性修饰词