一 下载phantomjs,把phantomjs.exe的文件路径加到环境变量中,也可以phantomjs.exe拷贝到一个已存在的环境变量路径中,比如我用的anaconda,我把phantomjs.exe文件加入到了Anaconda3这个文件夹中(Anaconda3已加入环境变量)

二 pip安装selenium+BeautifulSoup+phantomjs 命令pip install selenium,anaconda中已有BeautifulSoup,不用管

三 爬取数据,目标是爬取新浪新闻下的公司下面的所有的新闻文本。如图是新闻文章的列表,我们首先要抓取文章对用的链接,然后进入链接抓取文本

由于采用的是js加载的,如果直接用beautifulsoup是解析不出的,这里采用selenium+phantomjs抓取。抓取的思路是首先模拟点击公司新闻按钮,进入公司新闻栏目下,抓取该页所有新闻文章对应的链接,然后点击模拟点击下一页进入下一页循环抓取

下面是粗糙的代码实现:

from selenium import webdriver
from selenium.webdriver.common.by import By
from bs4 import BeautifulSoup
from urllib.request import urlopen
import re
import time
def get_links(driver):
'''
爬取链接并写入txt中
'''
t1 = time.time()
try:
driver.find_element(By.LINK_TEXT, "下一页").click()#每爬取完一页点击下一页
except NoSuchElementException:
time.sleep(1)
driver.find_element(By.LINK_TEXT, "下5页").click()#有可能遇到没有下一页,尝试点击下5页
time.sleep(1)
bs = BeautifulSoup(driver.page_source)#不知道怎么用selenium直接解析出href。把selenium的webdriver调用page_source函数在传入BeautifulSoup中,就可以用BeautifulSoup解析网页了
links = []
for i in bs.findAll('a',href=re.compile("http://finance.sina.com.cn/chanjing/gsnews/.")):#用正则表达式找出所有需要的链接
link = i.get('href')
if link not in links:#去掉重复链接
links.append(link)
f.write(link+'\n')
t2 = time.time()
page_num = bs.find('span',{'class','pagebox_num_nonce'}).text#找出当前页数
page_num = int(page_num)
if page_num>4:
return
print('爬取完第%d页,用时%d秒'%(page_num,t2-t1))
get_links(driver) def get_text(links,path):
'''
解析出所需文本,第一个参数为链接列表,第二个为保存路径
'''
n=0
for link in links:
html = urlopen(link)
bsObj = BeautifulSoup(html)
temp = ''
try:
for link in bsObj.find("div",{'id':re.compile('artibody')}).findAll('p'):
temp = temp+link.text.strip()#把每一段都拼接在一起
print(temp[:31])
path.write(temp+'\n')
n+=1
print('爬取完第%d篇'%n)
print('\n')
except (AttributeError,UnicodeEncodeError,UnicodeEncodeError):#这里的处理可能有点暴力
continue if True:#我把爬取的链接保存了下,所分成了两部,第一次爬取链接,第二次爬取文本
f = open('E:\hei.txt','w')
driver = webdriver.PhantomJS()#如果phantomjs.exe所在路径没有加入环境变量,这里也可以直接把其路径作为参数传给PhantomJS()
driver.get("http://finance.sina.com.cn/chanjing/")
driver.find_element(By.LINK_TEXT, "公司新闻").click()
time.sleep(2)
get_links(driver)
f.close()
driver.close() if True:#爬取文本
xl = open('E:\heiii.txt','w')
with open('E:\heii.txt') as f:
links = [link.strip() for link in f]
get_text(links,xl)

  

  

selenium+BeautifulSoup+phantomjs爬取新浪新闻的更多相关文章

  1. python3爬虫-爬取新浪新闻首页所有新闻标题

    准备工作:安装requests和BeautifulSoup4.打开cmd,输入如下命令 pip install requests pip install BeautifulSoup4 打开我们要爬取的 ...

  2. Python3:爬取新浪、网易、今日头条、UC四大网站新闻标题及内容

    Python3:爬取新浪.网易.今日头条.UC四大网站新闻标题及内容 以爬取相应网站的社会新闻内容为例: 一.新浪: 新浪网的新闻比较好爬取,我是用BeautifulSoup直接解析的,它并没有使用J ...

  3. Python 爬虫实例(7)—— 爬取 新浪军事新闻

    我们打开新浪新闻,看到页面如下,首先去爬取一级 url,图片中蓝色圆圈部分 第二zh张图片,显示需要分页, 源代码: # coding:utf-8 import json import redis i ...

  4. 【转】Python爬虫:抓取新浪新闻数据

    案例一 抓取对象: 新浪国内新闻(http://news.sina.com.cn/china/),该列表中的标题名称.时间.链接. 完整代码: from bs4 import BeautifulSou ...

  5. Python爬虫:抓取新浪新闻数据

    案例一 抓取对象: 新浪国内新闻(http://news.sina.com.cn/china/),该列表中的标题名称.时间.链接. 完整代码: from bs4 import BeautifulSou ...

  6. php使用pthreads v3多线程的抓取新浪新闻信息

    我们使用pthreads,来写一个多线程的抓取页面小程序,把结果存到数据库里. 数据表结构如下: CREATE TABLE `tb_sina` ( `id` int(11) unsigned NOT ...

  7. python2.7 爬虫初体验爬取新浪国内新闻_20161130

    python2.7 爬虫初学习 模块:BeautifulSoup requests 1.获取新浪国内新闻标题 2.获取新闻url 3.还没想好,想法是把第2步的url 获取到下载网页源代码 再去分析源 ...

  8. python爬取新浪股票数据—绘图【原创分享】

    目标:不做蜡烛图,只用折线图绘图,绘出四条线之间的关系. 注:未使用接口,仅爬虫学习,不做任何违法操作. """ 新浪财经,爬取历史股票数据 ""&q ...

  9. python3使用requests爬取新浪热门微博

    微博登录的实现代码来源:https://gist.github.com/mrluanma/3621775 相关环境 使用的python3.4,发现配置好环境后可以直接使用pip easy_instal ...

随机推荐

  1. RedisTemplate.htm

    http://docs.spring.io/spring-data/redis/docs/current/api/org/springframework/data/redis/core/RedisTe ...

  2. Mybatis入门 digest

    http://www.mybatis.org/mybatis-3/zh/configuration.html userDao-mapping.xml相当于是UserDao的实现, 同时也将User实体 ...

  3. finally块的问题(finally block does not complete normally) (转)

    当finall块中包含return语句时,Eclipse会给出警告“finally block does not complete normally”,原因分析如下: 1.不管try块.catch块中 ...

  4. Android Layout Binder(在线将XML中View find出来,生成java代码的工具)

    废话不多说,这是地址:http://android.lineten.net/layout.php. 有图有真相,比如: 你的XML假如是这样: <?xml version="1.0&q ...

  5. Error (0xc0000225) installing Windows 8 R2 on VirtualBox

    Windows Boot Manager Windows failed to start. A recent hardware or software change might be the caus ...

  6. STL: generate ,geterate_n

    在随机生成一定范围不重复数时用到random_shuffle函数,之前填充数组都是用for循环, 想到之前python中的range生成序列,于是在C++中找到对应的generate用来生成所需数组. ...

  7. BZOJAC400题留念

    BZOJAC400题了...

  8. Struts学习之值栈的理解

    转自:http://blog.csdn.net/hanxuemin12345/article/details/38559979 页面一个请求发送过来,依次经过一系列拦截器(处理公共部分,如:往数据中心 ...

  9. win使用telnet到ubuntu下vim显示中文为乱码的解决方法~

    1.几个路径: ubuntu: /etc/default/locale  相当于 centos:/etc/sysconfig/i18n vimrc的路径:① ~/.vimrc    ② /etc/vi ...

  10. win7 资源管理器的背景色修改

    主要参考 http://blog.sina.com.cn/s/blog_49c182c20100w3nb.html win7 通过dll修改背景色首先找到这个文件C:\Windows\Resource ...