暂无分类
暂无标签
发布于2020-04-23 09:09 阅读(279) 评论(0) 点赞(16) 收藏(2)
0
1
2
3
4
例如许飞的父亲写的散文诗
:https://music.163.com/#/song?id=417250673。
首先需要下载chromedrive,并需要在代码里指定路径,chromedrive版本号需要对应。
from selenium import webdriver
driver = webdriver.Chrome(executable_path='chromedriver.exe') # Chrome浏览器
driver.get("https://www.baidu.cn")
selenium是模拟人操作电脑的过程,类似游戏自动点击的脚本。流程十分简单:
XPath
来获取需要的内容# -*- coding=utf-8 -*-
from selenium import webdriver
import time
class YunSpider(object):
#初始化
def __init__(self,url):
self.url = url
self.driver = webdriver.Chrome(executable_path='chromedriver.exe')
#打开网站,实例方法
def getContent(self):
self.driver.get(self.url)
#进入内嵌网页
self.driver.switch_to.frame(0) #0代表是第一个框
js = 'window.scrollBy(0,8000)'
self.driver.execute_script(js)
#翻页
for page in range(2):
# //任意节点 .//从上个节点开始 /下面一个节点
selectors = self.driver.find_elements_by_xpath('//div[@class="cmmts j-flag"]/div')
for selector in selectors:
text = selector.find_element_by_xpath('.//div[@class="cnt f-brk"]').text
# print(text)
# self.saveData(text)
YunSpider.saveData(text)
#找到下一页的元素点击
# find_element_by_partial_link_text 获取文本链接,模糊匹配
nextPage = self.driver.find_element_by_partial_link_text('下一页')
# 点击下一页
nextPage.click()
time.sleep(0.5)
@staticmethod
def saveData(item):
with open('yun.txt','a',encoding='utf-8') as f:
f.write(item + '\n')
f.write('*'*20 + '\n')
if __name__ == '__main__':
url = 'https://music.163.com/#/song?id=417250673'
yunspder = YunSpider(url)
yunspder.getContent()
最后显示爬出的结果:
总结: 该例子很简单,复杂的内容爬取还需多学习。
原文链接:https://blog.csdn.net/lifei1229/article/details/105662254
0
1
2
3
4
5
6
7
8
9
作者:天青色等烟雨
链接: https://www.pythonheidong.com/blog/article/339444/3b6b1e3a22a979be43c5/
来源: python黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
Copyright © 2018-2021 python黑洞网 All Rights Reserved 版权所有,并保留所有权利。 京ICP备18063182号-1
投诉与举报,广告合作请联系z452as@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!