[Python爬虫] Selenium获取百度百科旅游景点的InfoBox消息盒
2023-09-14 08:56:52 时间
前面我讲述过如何通过BeautifulSoup获取维基百科的消息盒,同样可以通过Spider获取网站内容,最近学习了Selenium+Phantomjs后,准备利用它们获取百度百科的旅游景点消息盒(InfoBox),这也是毕业设计实体对齐和属性的对齐的语料库前期准备工作。希望文章对你有所帮助~
源代码
# coding=utf-8 Created on 2015-09-04 @author: Eastmount import time import re import os import sys import codecs from selenium import webdriver from selenium.webdriver.common.keys import Keys import selenium.webdriver.support.ui as ui from selenium.webdriver.common.action_chains import ActionChains #Open PhantomJS driver = webdriver.PhantomJS(executable_path="G:\phantomjs-1.9.1-windows\phantomjs.exe") #driver = webdriver.Firefox() wait = ui.WebDriverWait(driver,10) global info #全局变量 #Get the infobox of 5A tourist spots def getInfobox(name): try: #create paths and txt files global info basePathDirectory = "Tourist_spots_5A" if not os.path.exists(basePathDirectory): os.makedirs(basePathDirectory) baiduFile = os.path.join(basePathDirectory,"BaiduSpider.txt") if not os.path.exists(baiduFile): info = codecs.open(baiduFile,w,utf-8) else: info = codecs.open(baiduFile,a,utf-8) #locate input notice: 1.visit url by unicode 2.write files print name.rstrip(\n) #delete char \n driver.get("http://baike.baidu.com/") elem_inp = driver.find_element_by_xpath("//form[@id=searchForm]/input") elem_inp.send_keys(name) elem_inp.send_keys(Keys.RETURN) info.write(name.rstrip(\n)+\r\n) #codecs不支持\n换行 #print driver.current_url time.sleep(5) #load infobox elem_name = driver.find_elements_by_xpath("//div[@ ]/dl/dt") elem_value = driver.find_elements_by_xpath("//div[@ ]/dl/dd") #create dictionary key-value #字典是一种散列表结构,数据输入后按特征被散列,不记录原来的数据,顺序建议元组 elem_dic = dict(zip(elem_name,elem_value)) for key in elem_dic: print key.text,elem_dic[key].text info.writelines(key.text+" "+elem_dic[key].text+\r\n) time.sleep(5) except Exception,e: #utf8 codec cant decode byte print "Error: ",e finally: print \n info.write(\r\n) #Main function def main(): global info #By function get information source = open("Tourist_spots_5A_BD.txt",r) for name in source: name = unicode(name,"utf-8") if u故宫 in name: #else add a ? name = u北京故宫 getInfobox(name) print End Read Files! source.close() info.close() driver.close() main()
运行结果
主要通过从F盘中txt文件中读取国家5A级景区的名字,再调用Phantomjs.exe浏览器依次访问获取InfoBox值。同时如果存在编码问题“ascii codec cant encode characters”则可通过下面代码设置编译器utf-8编码,代码如下:
#设置编码utf-8 import sys reload(sys) sys.setdefaultencoding(utf-8) #显示当前默认编码方式 print sys.getdefaultencoding()
对应源码
其中对应的百度百科InfoBox源代码如下图,代码中基础知识可以参考我前面的博文或我的Python爬虫专利,Selenium不仅仅擅长做自动测试,同样适合做简单的爬虫。
编码问题
此时你仍然可能遇到“ascii codec cant encode characters”编码问题。
import codecs #用codecs提供的open方法来指定打开的文件的语言编码,它会在读取的时候自动转换为内部unicode if not os.path.exists(baiduFile): info = codecs.open(baiduFile,w,utf-8) else: info = codecs.open(baiduFile,a,utf-8) #该方法不是io故换行是\r\n info.writelines(key.text+":"+elem_dic[key].text+\r\n)
总结
你可以代码中学习基本的自动化爬虫方法、同时可以学会如何通过for循环显示key-value键值对,对应的就是显示的属性和属性值,通过如下代码实现:
elem_dic = dict(zip(elem_name,elem_value))
但最后的输出结果不是infobox中的顺序,why?
最后希望文章对你有所帮助,还有一篇基础介绍文章,但是发表时总会引发CSDN敏感系统自动锁定,而且不知道哪里引起的触发。推荐你可以阅读~
[python爬虫] Selenium常见元素定位方法和操作的学习介绍
(By:Eastmount 2015-9-6 深夜2点半 http://blog.csdn.net/eastmount/)
百度之星之F:百科蝌蚪团 百度百科有一支神奇的队伍,他们叫自己“百科蝌蚪团”。为了更好的让蝌蚪团的成员们安排工作,百度百科的运营团队定出了一个24小时制的时间表。
13、web爬虫讲解2—Scrapy框架爬虫—Scrapy爬取百度新闻,爬取Ajax动态生成的信息 crapy爬取百度新闻,爬取Ajax动态生成的信息,抓取百度新闻首页的新闻rul地址 有多网站,当你浏览器访问时看到的信息,在html源文件里却找不到,由得信息还是滚动条滚动到对应的位置后才...
OA系统里的知识管理,企业内部的“百度百科” 当企业意识到人才的重要性,很多企业开始思考择人、用人、育人、留人之道,而知识管理作为一项重要内容开始上升到企业的战略层面。
相关文章
- 试用百度云计算平台Python环境
- 第三百五十节,Python分布式爬虫打造搜索引擎Scrapy精讲—selenium模块是一个python操作浏览器软件的一个模块,可以实现js动态网页请求
- requests爬取百度贴吧:python 美女 3
- Python - 安装并配置Anaconda环境
- python:ERROR: No matching distribution found for Pillow==9.1.0的处理(Python 3.6.8)
- Python每日一练(20230227)
- paip.日志中文编码原理问题本质解决python
- 分享15个Python高级库
- Python语言学习:利用python获取当前/上级/上上级目录路径(获取路径下的最后叶目录的文件名、合并两个不同路径下图片文件名等目录/路径案例、正确加载图片路径)之详细攻略
- Python之tkinter:动态演示调用python库的tkinter带你进入GUI世界(Find/undo事件)
- 马上2023年了,如何高效自学python你还不知道吗?
- 【昇腾】【玩转Atlas200DK系列】为Atlas 200 DK制作python环境离线安装包
- 【Python成长之路】python 基础篇 -- 装饰器【华为云分享】
- Python编程:安装python环境以及virtualenv虚拟环境
- Python爬虫:利用百度短网址缩短url
- Python编程:通过百度文字识别提取表格数据
- 雅虎财经数据python 网络爬虫stock股票 用 Python 通过雅虎财经获取股票数据
- Python 极速入门指南
- Python自动化?软件测试工程师竟然用它干坏事!
- python spark 决策树 入门demo
- 多版本Python共存时pip给指定版本的python安装package的方法