JS网页信息的提取2(selenium的使用)

主要练习selenium的使用,获取由JS处理后的,网页信息。通过爬取某网页高考分数的情况来练习使用。

1. 数据库的安装和测试。

安装selenium

下载安装 PhantomJS

'''
pip install selenium
官网http://phantomjs.org/下载PhantomJS解压
'''

# 测试
from selenium import webdriver
url = 'http://www.baidu.com'
driver = webdriver.PhantomJS(executable_path="E:\python\phantomjs-2.1.1-windows\bin\phantomjs.exe")
driver.get(url)
data = driver.title  #  <title>百度一下,你就知道</title>
driver.save_screenshot('baidu.png')
print(data)

参考文章:

在Windows下安装PIP+Phantomjs+Selenium

2.信息提取的初步探究。

难点:对策

  1. 标签信息的过滤:判断语言(长度或者子标签)
  2. 标签文本信息的提取:基础知识的应用
  3. 将文本信息分类拆开
import re
from selenium import webdriver
from bs4 import BeautifulSoup
url = 'http://gkcx.eol.cn/soudaxue/queryProvince.html?page=1'

# 将js处理的,不能再源代码中看到的信息,还原处理 driver = webdriver.PhantomJS(executable_path="E:\python\phantomjs-2.1.1-windows\bin\phantomjs.exe") driver.get(url) data = driver.page_source # print(data) # 可以看到目标信息啦!!! # 做汤 soup = BeautifulSoup(data,'html.parser') tr = soup.find_all('tr') # 锁定标签 for all_info in tr:
# print(len(i)) 通过长度选好需要的数据 # 看看目标标签和其他标签的长度区别 if len(all_info) == 5: # 过滤出来长度是5的标签
#
if '<td>' in str(all_info): # 将标签内容字符化,通过子标签,过滤出需要的内容
#print(all_info.string)           # 文本信息提取命令 
        info_text=all_info.get_text()


        # 通过字符串的分割获得目标信息
        info_list = re.split('2017|本科|批', info_text)
        print(re.split('2017|本科|批',info_text))
        # 信息组合
        use_info = info_list[0],info_list[1],'本科'+info_list[2]+'',info_list[3]    # 正则分割,split只能分割一次
        print(use_info)

 全部代码:

# hanbb
# come on!!!
'''
pip install selenium
官网http://phantomjs.org/下载PhantomJS解压
'''
import re
from selenium import webdriver
from bs4 import BeautifulSoup
import csv

# 信息的存储
# 头部信息
file = open('E:\download2\daxue.csv', 'w', newline='')  # 打开的文件名称,追加模式,不写newline=''会出现行间距变大
writerfile = csv.writer(file)  # 写入命令
title = '地区','考生类别','批次','分数线'
writerfile.writerow(title)  # 写入内容
file.close()

def save(info):
    file = open('E:\download2\daxue.csv', 'a', newline='')  # 打开的文件名称,追加模式,不写newline=''会出现行间距变大
    writerfile = csv.writer(file)  # 写入命令
    writerfile.writerow(info)  # 写入内容
    file.close()  # 关闭文件

def get_info(url):
    #  还原js隐藏的信息
    driver = webdriver.PhantomJS(executable_path="E:\python\phantomjs-2.1.1-windows\bin\phantomjs.exe")
    driver.get(url)
    data = driver.page_source

    # 做汤
    soup = BeautifulSoup(data,'html.parser')
    tr = soup.find_all('tr')

    for all_info in tr:
        # 筛选标签
        if '<td>' in str(all_info):
            info_text=all_info.get_text()

            if len(info_text)==14:       # 针对特别的省份
                use_info = 0,0,0,0

            elif len(info_text) == 15 or 16:
                # 通过字符串的分割获得目标信息
                info_list = re.split('2017|本科|批', info_text)
                # print(info_list)
                # 信息组合
                use_info = info_list[0],info_list[1],'本科'+info_list[2]+'',info_list[3]
                # save(use_info)
            else:
                use_info = 0,0,0,0

            save(use_info)
            print('信息写入')
                #print(use_info)

if __name__ == '__main__':
    for page in range(1,10):
        url = 'http://gkcx.eol.cn/soudaxue/queryProvince.html?page={}'.format(page)
        get_info(url)
        print(page)

不足至处:运行慢;有些省份存在特殊情况,处理的时候只0,0,0,0 不然会报错

参考文献:

爬虫再探实战(三)———爬取动态加载页面——SELENIUM

原文地址:https://www.cnblogs.com/hanbb/p/8022537.html