维普__获取页面简介
来源:互联网 发布:医疗his软件 编辑:程序博客网 时间:2024/05/22 03:43
#!/usr/bin/python#encoding=utf-8__author__ = 'Administrator'from bs4 import BeautifulSoupimport seleniumimport sysimport urllibimport requestsimport timeimport reimport csvif __name__ == "__main__": import os from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0'} chromedriver = "/home/henson/Documents/pycharm/webdriver/chromedriver" os.environ["webdriver.chrome.driver"] = chromedriver driver = webdriver.Chrome(chromedriver) driver.execute_script("window.scrollTo(0, document.body.scrollHeight);") driver.get('http://qikan.cqvip.com/zk/search.aspx?key=M%3d%E5%A4%A7%E6%B0%94%E6%B1%A1%E6%9F%93%E9%98%B2%E6%B2%BB&page=5&ids=') #inputElement = driver.find_element_by_xpath("//*[@id='b_Text0']")#//*[@id="b_Text0"] urlList=[] f = open("/home/henson/Documents/coding/pachong/vp_ms.csv", "a+", encoding='utf-8') writer = csv.writer(f) writer.writerow(["title", "author", "cited", "fund"]) for i in range(1,300): for j in range(1, 100): try: currentURL = driver.current_url now_handle = driver.current_window_handle # 获取当前窗口句柄 all_handles = driver.window_handles # 获取所有窗口句柄 for handle in all_handles: if (handle != now_handle): driver.switch_to_window(handle) # print("new web"+ driver.current_url) title = driver.find_element_by_xpath( '//*[@id="body"]/div/div[3]/div[4]/dl[' + str(j) + ']/dt/a').text author = driver.find_element_by_xpath( '//*[@id="body"]/div/div[3]/div[4]/dl[' + str(j) + ']/dd[3]/[@class="writer"]').text cited = driver.find_element_by_xpath( '//*[@id="body"]/div/div[3]/div[4]/dl[' + str(j) + ']/dt/[@class="cited"]').text fund = driver.find_element_by_xpath( '//*[@id="body"]/div/div[3]/div[4]/dl[' + str(j) + ']/dd[4]/[@class=""]').text print(title) print(author) print(cited) print(fund) writer.writerows([title, author, cited, fund]) #写入CSV except Exception: try: title = driver.find_element_by_xpath( '//*[@id="body"]/div/div[3]/div[4]/dl[' + str(j) + ']/dt/a').text author = driver.find_element_by_xpath( '//*[@id="body"]/div/div[3]/div[4]/dl[' + str(j) + ']/dd[3]/span[2]').text cited = driver.find_element_by_xpath( '//*[@id="body"]/div/div[3]/div[4]/dl[' + str(j) + ']/dt/span/a').text fund = driver.find_element_by_xpath('//*[@id="body"]/div/div[3]/div[4]/dl[9]/dd[4]/span').text continue; except Exception: break; break; now_handle = driver.current_window_handle # 获取当前窗口句柄 all_handles = driver.window_handles # 获取所有窗口句柄 driver.find_element_by_xpath( '//*[@id="body"]/div/div[3]/div[6]/div[2]/span[2]/a[2]').click() # 下一页 time.sleep(2) currentURL = driver.current_url f.close()
思维的定性让我想着要是没有的数据Xpath找不到的元素直接能跳过,却从未认真观察分析其中的构造。其实稍微观察就能发现,很多东西都是有一定的格式和规律的,如果内容为null,但是它的格式还在那,所以完全没有必要去想有没有可以直接跳过的Xpath,即使没有文本内容,很多情况下,它的标签格式是一定的,它一定都还在哪里。后知后觉——
阅读全文
0 0
- 维普__获取页面简介
- HTML5(1)__简介
- 909422229__正则表达式简介
- 909422229__正则表达式简介
- pandas基础__之__对获取mongodb数据
- __
- OSCache 学习(3)__缓存整个页面 & 缓存局部页面
- xpath简介__用于在XML中查找
- 2016.3.15__H5页面实战__第七天
- robot脚本__获取当前弹出窗体标题的讨论
- java基础__获取键盘输入值(了解即可)
- 获取鼠标点击坐标等__颜色选择器的制作
- 909422229__关于JS_Element获取元素与属性的总结
- WEB 页面简介
- 获取来源页面的页面
- OSCache 学习笔记(2)__缓存整个页面 & 缓存局部页面
- 简介获取图片
- 获取页面的HTML
- break语句
- hdu 6092 Rikka with Subset 背包dp
- 数据结构之栈和队列
- Python学习之基础知识
- github上的awesome
- 维普__获取页面简介
- 分数拆分【南阳oj 题目66】
- [Python]在python中获取函数的参数名和参数名的默认值
- HDU 6095 Rikka with Competition
- 【linux】cat命令
- ucbug造梦西游5修改器使用方法介绍
- hdu6069
- 4.gradient descent
- hdu 6090 Rikka with Graph