记录一次快速实现的python爬虫,想要抓取中财网数据引擎的新三板板块下面所有股票的公司档案,网址为http://data.cfi.cn/data_ndkA0A1934A1935A1986A1995.html。

  比较简单的网站不同的页码的链接也不同,可以通过观察链接的变化找出规律,然后生成全部页码对应的链接再分别抓取,但是这个网站在换页的时候链接是没有变化的,因此打算去观察一下点击第二页时的请求

  发现使用的是get的请求方法,并且请求里有curpage这个参数,貌似控制着不同页数,于是改动了请求链接中的这个参数值为其他数值发现并没有变化,于是决定换一种方法,就是我们标题中提到的使用selenium+beautifulsoup实现模拟点击网页中的下一页按钮来实现翻页,并分别抓取网页里的内容。

  首先我们先做一下准备工作,安装一下需要的包,打开命令行,直接pip install selenium和pip install beautifulsoup4

  然后就是下载安装chromedriver的驱动,网址如下https://sites.google.com/a/chromium.org/chromedriver/downloads,记得配置下环境变量或者直接安装在工作目录下。(还可以使用IE、phantomJS等)

  这里我们先抓取每一个股票对应的主页链接,代码如下(使用python2):

 1 # -*- coding: utf-8 -*-
2 from selenium import webdriver
3 from bs4 import BeautifulSoup
4 import sys
5 reload(sys)
6 sys.setdefaultencoding('utf-8')
7
8 def crawl(url):
9 driver = webdriver.Chrome()
10 driver.get(url)
11 page = 0
12 lst=[]
13 with open('./url.txt','a') as f:
14 while page < 234:
15 soup = BeautifulSoup(driver.page_source, "html.parser")
16 print(soup)
17 urls_tag = soup.find_all('a',target='_blank')
18 print(urls_tag)
19 for i in urls_tag:
20 if i['href'] not in lst:
21 f.write(i['href']+'\n')
22 lst.append(i['href'])
23 driver.find_element_by_xpath("//a[contains(text(),'下一页')]").click()
24 time.sleep(2)
25 return 'Finished'
26 def main():
27 url = 'http://data.cfi.cn/cfidata.aspx?sortfd=&sortway=&curpage=2&fr=content&ndk=A0A1934A1935A1986A1995&xztj=&mystock='
28 crawl(url)
29 if __name__ == '__main__':
30 main()

    运行代码发现总是报错:

    这里报错的意思是找不到想要找的按钮。

    于是我们去查看一下网页源代码:

    发现网页分为不同的frame,所以我们猜想应该需要跳转frame,我们需要抓取的链接处于的frame的name为“content”,所以我们添加一行代码:driver.switch_to.frame('content')

def crawl(url):
driver = webdriver.Chrome()
driver.get(url)
driver.switch_to.frame('content')
page = 0
lst=[]
with open('./url.txt','a') as f:
while page < 234:
soup = BeautifulSoup(driver.page_source, "html.parser")
print(soup)
urls_tag = soup.find_all('a',target='_blank')
print(urls_tag)
for i in urls_tag:
if i['href'] not in lst:
f.write(i['href']+'\n')
lst.append(i['href'])
driver.find_element_by_xpath("//a[contains(text(),'下一页')]").click()
time.sleep(2)
return 'Finished'

    至此,运行成:

参考博文链接:          http://unclechen.github.io/2016/12/11/python%E5%88%A9%E7%94%A8beautifulsoup+selenium%E8%87%AA%E5%8A%A8%E7%BF%BB%E9%A1%B5%E6%8A%93%E5%8F%96%E7%BD%91%E9%A1%B5%E5%86%85%E5%AE%B9/

http://www.cnblogs.com/liyuhang/p/6661835.html

使用selenium webdriver+beautifulsoup+跳转frame,实现模拟点击网页下一页按钮,抓取网页数据的更多相关文章

  1. 使用Selenium+firefox抓取网页指定firefox_profile后的问题

    from: https://blog.csdn.net/chufazhe/article/details/51145834 摘要:在使用selenium和firefox抓取网页指定firefox_pr ...

  2. [Python爬虫] 之八:Selenium +phantomjs抓取微博数据

    基本思路:在登录状态下,打开首页,利用高级搜索框输入需要查询的条件,点击搜索链接进行搜索.如果数据有多页,每页数据是20条件,读取页数 然后循环页数,对每页数据进行抓取数据. 在实践过程中发现一个问题 ...

  3. [Python爬虫] 之四:Selenium 抓取微博数据

    抓取代码: # coding=utf-8import osimport refrom selenium import webdriverimport selenium.webdriver.suppor ...

  4. selenium配合phantomjs实现爬虫功能,并把抓取的数据写入excel

    # -*- coding: UTF-8 -*- ''' Created on 2016年5月13日 @author: csxie ''' import datetime from Base impor ...

  5. 利用selenium抓取网页的ajax请求

    部门需要一个自动化脚本,完成web端界面功能的冒烟,并且需要抓取加载页面时的ajax请求,从接口层面判断请求是否成功.查阅了很多资料都没有人有过相关问题的处理经验,在处理过程中也踩了很多坑,所以如果你 ...

  6. php使用curl抓取网页自动跳转问题处理

    问题分析: 请求抓取http://go.com数据: function curlGet($url) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, ...

  7. python+selenium+webdriver+BeautifulSoup实现自动登录

    from selenium import webdriverimport timefrom bs4 import BeautifulSoupfrom urllib import requestimpo ...

  8. 一、使用 BeautifulSoup抓取网页信息信息

    一.解析网页信息 from bs4 import BeautifulSoup with open('C:/Users/michael/Desktop/Plan-for-combating-master ...

  9. Selenium webdriver 截图 太长截不全的问题

    Selenium webdriver 截图 太长截不全的问题 1.环境 selenium webdriver.net 2.46.0.0 + firefox 37.0.1 + win 8.1 2.问题 ...

随机推荐

  1. linux系统命令<二>----du的使用方法

    1> 要显示一个目录树及其每个子树的磁盘使用情况 du /home/linux 这在/home/linux目录及其每个子目录中显示了磁盘块数. 2> 要通过以1024字节为单位显示一个目录 ...

  2. Struts2第十一篇【简单UI标签、数据回显】

    Struts2UI标签 Sturts2为了简化我们的开发,也为我们提供了UI标签-也就是显示页面的标签-.. 但是呢,Struts2是服务端的框架,因此使用页面的标签是需要在服务器端解析然后再被浏览器 ...

  3. 监控-CPU使用率

    原始脚本来自TG,自己对部分脚本做了调整,分享出来仅供参考,请勿整篇Copy! 使用以下语句获取[CPU使用率] USE [DBA_Monitor] GO /****** 对象: StoredProc ...

  4. hadoop2.x的变化

    HDFS Federation(HDFS联邦) HDFS有两个主要层: Namespace 由目录.文件和块组成:支持所有命名空间对文件和目录的操作. Block Storage Service 由B ...

  5. Java http请求和调用

    关于http get和post请求调用代码以及示例. 参考:http://www.cnblogs.com/zhuawang/archive/2012/12/08/2809380.html http请求 ...

  6. 基于c编写的关于随机生成四则运算的小程序

    基于http://www.cnblogs.com/HAOZHE/p/5276763.html改编写的关于随机生成四则运算的小程序 github源码和工程文件地址:https://github.com/ ...

  7. javascript 单元测试初入门

    1.使用mocha工具实现单元测试 ①首先准备node环境 ②安装mocha:npm install mocha 也可以进行全局安装 npm install global mocha ③安装断言库:n ...

  8. 翻译 | 一行 JavaScript 代码的逆向工程

    原文地址:Reverse Engineering One Line of JavaScript 原文作者:Alex Kras 译者:李波 校对者:冬青.小萝卜 几个月前,我看到一个邮件问:有没有人可以 ...

  9. 关于DbContext能不能单次请求内唯一?DbContex需不需要主动释放?欢迎各路大侠来“参战”!

    基于前篇文章<HiBlogs>重写笔记[1]--从DbContext到依赖注入再到自动注入园友@Flaming丶淡蓝@ 吴瑞祥 提出了讨论和质疑,吓得我连夜查询资料(玩笑~). 本来文章的 ...

  10. tomcat部署在centos6.8上的乱码问题

    web访问经常会莫名其妙的出现各种乱码问题.按照我自己的理解,设置一个charSet的过滤器,代码如下:import java.io.IOException; import javax.servlet ...