python第二次采集数据小记
来源:互联网 发布:杭州淘宝学院 编辑:程序博客网 时间:2024/05/16 09:25
有些网页右键查看网页源代码,里面没有要查找的数据,这是为什么呢?答案是:页面是由JS动态生成出来的。
但是在审查元素中Elements中是有的。
解决方案是 python 有一个第三方库 Selenium 可以模拟浏览器
第一步 安装 Selenium
在 cmd 中 打开python的Scripts目录 。输入python 回车
输入 : pip install selenium
安装最新版Selenium
第二步 安装浏览器驱动(本人使用的Chrome)
网上找到与浏览器对应版本的驱动后(谷歌为 chromedriver ),下载解压 放到浏览器的安装目录下 (chrome://version 命令查看路径)
第三步 代码部分
chromedriver = "C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe"#驱动路径driver = webdriver.Chrome(chromedriver)#启动浏览器driver.get("url")#打开网址
使用time函数 需要import time 导入
第四部 使用PhantomJS替代浏览器
Phantom JS是一个服务器端的 JavaScript API 的 WebKit。其支持各种Web标准: DOM 处理, CSS 选择器, JSON, Canvas, 和 SVG。
下载 Phantomjs后解压 把phantomjs.exe拷贝到python的scripts目录下
使用示例:
#调用无窗口 phantomJS driver = webdriver.PhantomJS() driver.get(url)
下面是完整实例代码:
#-*-coding:utf-8-*-##导入需要的类库import requestsimport urlparseimport reimport sysimport timeimport MySQLdbfrom bs4 import BeautifulSoupfrom selenium import webdriver#构造请求头headers = { "Accept":"text/html,application/xhtml+xml,application/xml;", "Accept-Encoding":"gzip", "Accept-Language":"zh-CN,zh;q=0.8", "Referer":"http://www.example.com/", "User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36" }conn= MySQLdb.connect( host='localhost', port = 3306, user='root', passwd='123', db ='caiji', )cur = conn.cursor()sql=cur.execute("select * from xyyk")info = cur.fetchmany(sql)for i in info: url = i[2] numid = i[0] #print url #调用谷歌浏览器 #chromedriver = "C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe" #driver = webdriver.Chrome(chromedriver) #调用无窗口 phantomJS driver = webdriver.PhantomJS() driver.get(url) time.sleep(25) soup = BeautifulSoup(driver.page_source) try: for j in soup.find('div', {'class': 'youkexx-box youkexx-box2'}).find_all('input'): print j['value'] value = j['value'] except Exception,e: print '写入失败 numid' else: #print value #print numid cur.execute("update xyyk set newUrl = '%s' where id = '%s'" %(value,numid)) driver.quit() time.sleep(5)cur.close()conn.commit()conn.close()
阅读全文
0 0
- python第二次采集数据小记
- Python第一次采集数据小记
- python第三次采集数据小记之登录下载
- Python 采集数据
- python网络数据采集
- python 数据采集入门
- Python遇见数据采集
- Python网络数据采集
- Python网络数据采集
- Python网络数据采集
- Python网络数据采集
- Python网络数据采集
- Python网络数据采集
- Python网络数据采集
- Python网络数据采集
- Python网络数据采集
- Python网络数据采集
- python 数据初学者 小记
- Inventor: 获取电缆管路部件的名字
- 一些bash脚本练习题
- 【TK】1012: 外币兑换
- Python list []
- 模拟量读取
- python第二次采集数据小记
- ubuntu14.04 mysql升级到5.7版本出现GPG error错误解决办法
- Python tuple
- Spring第一天
- 海思编解码芯片添加64M nor flash
- 基于比较的排序算法复杂度为nlg(n)的证明
- 【Linux排错】wget_insecurely
- eclipse+maven+springboot小Demo
- Python 条件判断