selenium+phantomjs+xpath用法

from selenium import webdriver #导入python版的selenium(webdriver) import time from selenium.webdriver.common.desired_capabilities import DesiredCapabilities print(“time:”,time.strftime(’%Y-%m-%d %H:%M:%S’,time.localtime(time.time()))) ‘’’ 基于PhantomJS创建一个无界面浏览器,并且设置一下用户代理, 否则可能出现界面不兼容的情况desired_capabilities=dcap ‘’’ dcap = dict(DesiredCapabilities.PHANTOMJS) dcap[“phantomjs.page.settings.userAgent”] = (“Mozilla/4.0 (compatible; MSIE 5.5; windows NT)” ) browser = webdriver.PhantomJS() ‘’’ 我们通过PhantomJS打开相关 http://ac.qq.com/ComicView/index/id/539443/cid/1 ‘’’ print(“time:”,time.strftime(’%Y-%m-%d %H:%M:%S’,time.localtime(time.time()))) browser.get(‘https://www.cbirc.gov.cn/cn/view/pages/ItemList.html?itemPId=923&itemId=925&itemUrl=ItemListRightList.html&itemName=%E5%85%AC%E5%91%8A%E9%80%9A%E7%9F%A5’) print(“time:”,time.strftime(’%Y-%m-%d %H:%M:%S’,time.localtime(time.time()))) print(“title:”,browser.title) #获取标题《宛香》遇见(1)-在线漫画-腾讯动漫官方网站 print(browser.page_source) #打印当前网页所有源代码 # print(browser.find_element_by_xpath()) print(“time:”,time.strftime(’%Y-%m-%d %H:%M:%S’,time.localtime(time.time()))) print(“获得目标位置:”) # print(browser.find_element_by_class_name(‘ng-scope’)) first_container = browser.find_elements_by_xpath("//div[@ng-if=\“itemName!=‘政府网站年度报表’\” and @class=\“ng-scope\”]/div/div/div/div[@class=\“panel-row ng-scope\”]") print(“time:”,time.strftime(’%Y-%m-%d %H:%M:%S’,time.localtime(time.time()))) print(“len of list_container:”,len(first_container)) for first_element in first_container: print(“title:”) print(first_element.text) print(first_element.get_attribute(“ng-repeat”)) print(“second container:”) #print(first_element.find_element_by_class_name()) second_container_name = first_element.find_element_by_xpath(".//span[@class=\“title\”]") print(“name:”,second_container_name.text) second_container_a = first_element.find_element_by_xpath(".//span[@class=\“title\”]/a") print(“a.attributes:”,second_container_a.get_attribute(“href”)) second_container_date = first_element.find_element_by_xpath(".//span[@class=\“date ng-binding\”]") print(“date:”,second_container_date.text)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小金子的夏天

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值