Python使用Selenium+ChromeDriver+BeautifulSoup实现淘宝产品详情页数据爬取
2019-07-08 18:42
756 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_44186072/article/details/95080789
一、下载ChromeDriver
注意版本与Selenium对应,如果不清楚运行代码时会报错提示,只需要找到对应版本就行。
下载地址: http://npm.taobao.org/mirrors/chromedriver/
二、导入资源包
from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.support.wait import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from bs4 import BeautifulSoup
三、核心代码
if __name__ == '__main__': chrome_options = Options() # 设置chrome浏览器无界面模式 chrome_options.add_argument('lang=zh_CN.UTF-8') # 设置中文 # chrome_options.add_argument('window-size=1920x3000') # 指定浏览器分辨率 chrome_options.add_argument('--disable-gpu') # 谷歌文档提到需要加上这个属性来规避bug # chrome_options.add_argument('--hide-scrollbars') # 隐藏滚动条, 应对一些特殊页面 chrome_options.add_argument('blink-settings=imagesEnabled=false') # 不加载图片, 提升速度 chrome_options.add_argument('--headless') # 浏览器不提供可视化页面. linux下如果系统不支持可视化不加这条会启动失败 # 设置手机请求头 (手机页面反爬虫能力稍弱) chrome_options.add_argument( 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.157 Mobile Safari/537.36') # 加载设置 browser = webdriver.Chrome(options=chrome_options) url = "https://item.taobao.com/item.htm?spm=a230r.1.14.11.2e677cabjgUgYf&id=587148479406&ns=1&abbucket=2#detail" # 开始请求 browser.get(url) # 获取页面内容 html = BeautifulSoup(browser.page_source, 'html.parser') # 选择有效数据 shop_name = html.select('.tb-shop-name a') if shop_name: shop_name = shop_name[0].get('title') else: shop_name = '' print('店铺:' + str(shop_name)) # 关闭浏览器 browser.close() # 关闭chreomedriver进程 browser.quit()
注意:
1、ChromeDriver 直接拷贝到资源目录下就可自动启动
2、BeautifulSoup具体复杂操作请参考文档
地址:https://www.crummy.com/software/BeautifulSoup/bs3/documentation.zh.html
3、Selenium还具有显示等待和隐示等待,可以解决ajax渲染的数据无法爬取问题。具体查看文档
地址:https://www.seleniumhq.org/docs/cn/
相关文章推荐
- Python爬取淘宝搜索页,使用Selenium+BeautifulSoup
- Python使用Selenium+BeautifulSoup爬取淘宝搜索页
- Python使用Selenium爬取淘宝异步加载的数据方法
- python+BeautifulSoup+selenium+mysqldb完成数据抓取
- Python使用Selenium爬取淘宝异步加载的数据
- Python使用Selenium webdriver爬虫某电商商品数据
- 使用selenium的WebDriver和ChromeDriver实现UI自动化
- Python使用Selenium模块实现模拟浏览器抓取淘宝商品美食信息功能示例
- 使用requests+beautifulsoup模块实现python网络爬虫功能
- 使用python抓取并分析数据—链家网(requests+BeautifulSoup)(转)
- python3实现爬取淘宝页面的商品的数据信息(selenium+pyquery+mongodb)
- Liunx环境下python使用: Headless chrome + Selenium + ChromeDriver搭建
- [Python+Selenium]Mac OS下应用Homebrew完成Chromedriver安装以及使用selenium完成百度搜索并显示搜索结果
- python+selenium+mongodb实现爬取京东网站特定关键词产品数据
- Python抓取网页动态数据——selenium webdriver的使用
- 【Python3.6爬虫学习记录】(七)使用Selenium+ChromeDriver爬取知乎某问题的回答
- 使用Python+selenium+BeautifulSoup抓取动态网页的关键信息
- HOW TO: Install Chromedriver for Mac OS X / Selenium / Python
- python实现zencart产品数据导入到magento(python导入数据)
- 使用Python实现子区域数据分类统计