# -*- coding: utf-8 -*-
import time
import json
import tempfile
from PIL import Image
from selenium import webdriver
# 要想调用键盘按键操作需要引入keys包
from selenium.webdriver.common.keys import Keys
class CreditSpider(object):
def __init__(self):
# 调用环境变量指定的PhantomJS浏览器创建浏览器对象
self.driver = webdriver.PhantomJS()
# self.driver.maximize_window() # 设置全屏
# 如果没有在环境变量指定PhantomJS位置
# self.driver = webdriver.PhantomJS(executable_path='/Users/bianyachao/Downloads/phantomjs-2.1.1-macosx/bin/phantomjs')
self.driver.set_window_size(1366, 1098)
def get_credit(self):
url = "http://hd.chinatax.gov.cn/fagui/action/InitCredit.do"
# get方法会一直等到页面加载,然后才会继续程序,通常测试会在这里选择time.sleep(2)
self.driver.get(url)
time.sleep(2)
# 生成页面快照并保存
# self.driver.save_screenshot("nsrxy.png")
self.on_click(1)
def on_click(self, n):
while n < 3:
if n == 1:
self.driver.find_element_by_xpath("""//a[@onclick="changeParam('articleField01','')"]""").click()
else:
self.driver.find_element_by_xpath('//*[@title="下一页"]').click()
time.sleep(2)
self.driver.save_screenshot("yzm.png")
self.jietu_img(n)
self.click_yzm(n)
n += 1
return
def click_yzm(self, n):
print('the path is {}'.format(n))
element = self.driver.find_element_by_id("verifyCode")
self.driver.find_element_by_id("verifyCode").clear()
# im = Image.open('codeImage.png')
# im.show()
yzm = input("please input code: ")
element.send_keys(yzm)
self.driver.find_element_by_xpath("""//*[@id="layui-layer1"]/div[3]/a[1]""").click()
time.sleep(4)
# self.driver.save_screenshot("result{}.png".format(n))
self.parse_page(n)
return
def parse_page(self, n):
try:
res_list = []
data = self.driver.find_elements_by_xpath('//td[@class="sv_hei"]//tr/td')
if not data:
self.on_click(n)
for a in data:
d = a.text
res_list.append(d)
self.save_res(res_list)
except Exception as e:
print(e)
return
def save_res(self, res_list):
cont_list = []
del res_list[-1]
for i in range(0, 3):
del res_list[0]
for i in range(0, len(res_list), 3):
res_dic = {
'NSSBH': res_list[i],
'NSRMC': res_list[i+1],
'YEAR': res_list[i+2],
}
cont_list.append(res_dic)
print(cont_list)
# json_str = json.dumps(cont_list)
# with open('res.text', 'a+') as f:
# f.write(json_str + '\n\n')
return
def jietu_img(self, n):
# 参数说明
# 第一个参数 开始截图的x坐标
# 第二个参数 开始截图的y坐标
# 第三个参数 结束截图的x坐标
# 第四个参数 结束截图的y坐标
png = Image.open('yzm.png')
if n == 1:
bbox = (531, 510, 731, 560)
else:
bbox = (531, 512, 731, 562)
region = png.crop(bbox) # 此时,region是一个新的图像对象
time.sleep(1)
region.save('codeImage.png')
return
if __name__ == '__main__':
s = time.time()
a = CreditSpider()
a.get_credit()
# a.jietu_img_2()
print(time.time()-s)
报错:
Traceback (most recent call last):
File "credit_spider.py", line 122, in <module>
a.get_credit()
File "credit_spider.py", line 31, in get_credit
self.on_click(1)
File "credit_spider.py", line 36, in on_click
self.driver.find_element_by_xpath("""//a[@onclick="changeParam('articleField01','')"]""").click()
File "/root/anaconda3/lib/python3.6/site-packages/selenium/webdriver/remote/webelement.py", line 80, in click
self._execute(Command.CLICK_ELEMENT)
File "/root/anaconda3/lib/python3.6/site-packages/selenium/webdriver/remote/webelement.py", line 501, in _execute
return self._parent.execute(command, params)
File "/root/anaconda3/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 308, in execute
self.error_handler.check_response(response)
File "/root/anaconda3/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 194, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotVisibleException: Message: {"errorMessage":"Element is not currently visible and may not be manipulated","request":{"headers":{"Accept":"application/json","Accept-Encoding":"identity","Connection":"close","Content-Length":"81","Content-Type":"application/json;charset=UTF-8","Host":"127.0.0.1:48854","User-Agent":"Python http auth"},"httpVersion":"1.1","method":"POST","post":"{\"id\": \":wdc:1531296937655\", \"sessionId\": \"9443d970-84e2-11e8-9955-092f7358cfea\"}","url":"/click","urlParsed":{"anchor":"","query":"","file":"click","directory":"/","path":"/click","relative":"/click","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/click","queryKey":{},"chunks":["click"]},"urlOriginal":"/session/9443d970-84e2-11e8-9955-092f7358cfea/element/:wdc:1531296937655/click"}}
Screenshot: available via screen
这是什么原因?求大神指导,谢谢了!!
linux服务器,你的PhantomJS是mac版本?