Python中怎么利用selenium实现一个动态爬虫,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。
1. 安装
selenium安装比较简单,直接用pip就可以安装,打开cmd,输入
pip install selenium
就好了
2. 安装chromedriver
chromedriver是谷歌浏览器的驱动程序,因为我平时用chrome
这里需要注意的是,chromedriver的版本需要是你安装的Chrome的版本对应起来,Chrome的版本可以在浏览器的右上角找到帮助-关于Google Chrome 查看浏览器的版本。具体的对应规则如下:
chromedriver版本 | 支持的Chrome版本 |
---|---|
v2.40 | v66-68 |
v2.39 | v66-68 |
v2.38 | v65-67 |
v2.37 | v64-66 |
v2.36 | v63-65 |
v2.35 | v62-64 |
v2.34 | v61-63 |
v2.33 | v60-62 |
v2.32 | v59-61 |
v2.31 | v58-60 |
v2.30 | v58-60 |
v2.29 | v56-58 |
v2.28 | v55-57 |
v2.27 | v54-56 |
v2.26 | v53-55 |
v2.25 | v53-55 |
v2.24 | v52-54 |
v2.23 | v51-53 |
v2.22 | v49-52 |
安装完之后,把驱动的安装目录添加到系统Path中就好了,如果不添加,在运行程序的时候就会报错,提示你没有添加到Path中。
3. 开始爬虫
今天要爬取的网址是:https://www.upbit.com/service_center/notice,然后点击翻页按钮,发现url并没有变化,通过F12查看请求的地址变化,可以发现,
https://www.upbit.com/service_center/notice?id=1
这里主要变化的就是后面的id,1,2,3,。。。依次类推。
用selenium爬虫开始前,需要定义好下面内容
# 设置谷歌浏览器的选项,opt = webdriver.ChromeOptions()# 将浏览器设置为无头浏览器,即先爬虫时,没有显示的浏览器opt.set_headless()# 浏览器设置为谷歌浏览器,并设置为上面设置的选项browser = webdriver.Chrome(options=opt)save = []home = 'https://www.upbit.com/home'# 创建好浏览器对象后,通过get()方法可以向浏览器发送网址,# 获取网址信息browser.get(home)time.sleep(15)
然后是如何定位html的元素,在selenium中,定位元素的方法有
find_element_by_id(self, id_)
find_element_by_name(self, name)
find_element_by_class_name(self, name)
find_element_by_tag_name(self, name)
find_element_by_link_text(self, link_text)
find_element_by_partial_link_text(self, link_text)
find_element_by_xpath(self, xpath)
find_element_by_css_selector(self, css_selector)
其中的id,name等都可以通过浏览器获得,定位元素的目的是为了获取我们想要的信息,然后解析出来保存,通过调用tex方法可以获得元素的文本信息。
下面把整个爬虫的代码,贴出来,供大家参考
from selenium import webdriverimport timefrom tqdm import trangefrom collections import OrderedDictimport pandas as pddef stringpro(inputs): inputs = str(inputs) return inputs.strip().replace("\n", "").replace("\t", "").lstrip().rstrip()opt = webdriver.ChromeOptions()opt.set_headless()browser = webdriver.Chrome(options=opt)save = []home = 'https://www.upbit.com/home'browser.get(home)time.sleep(15)for page in trange(500): try: rows = OrderedDict() url = "https://www.upbit.com/" \ "service_center/notice?id={}".format(page) browser.get(url) content = browser.find_element_by_class_name( name='txtB').text title_class = browser.find_element_by_class_name( name='titB') title = title_class.find_element_by_tag_name( 'strong').text times_str = title_class.find_element_by_tag_name( 'span').text times = times_str.split('|')[0].split(" ")[1:] num = times_str.split("|")[1].split(" ")[1] rows['title'] = title rows['times'] = " ".join(times) rows['num'] = num rows['content'] = stringpro(content) save.append(rows) print("{},{}".format(page, rows)) except Exception as e: continuedf = pd.DataFrame(save)df.to_csv("./datasets/www_upbit_com.csv", index=None)
看完上述内容,你们掌握Python中怎么利用selenium实现一个动态爬虫的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
原文链接:https://my.oschina.net/alvinpy/blog/4392071