[Python爬虫] Selenium获取百度百科旅游景点的InfoBox消息盒
生活随笔
收集整理的這篇文章主要介紹了
[Python爬虫] Selenium获取百度百科旅游景点的InfoBox消息盒
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
? ? ? ? 前面我講述過如何通過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 filesglobal infobasePathDirectory = "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'換行time.sleep(2)print driver.current_urlprint driver.title#load infobox basic-info cmn-clearfixelem_name = driver.find_elements_by_xpath("//div[@class='basic-info cmn-clearfix']/dl/dt") elem_value = driver.find_elements_by_xpath("//div[@class='basic-info cmn-clearfix']/dl/dd")for e in elem_name:print e.textfor e in elem_value:print e.text#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 can't 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 can't encode characters”則可通過下面代碼設置編譯器utf-8編碼,代碼如下:
#設置編碼utf-8 import sys reload(sys) sys.setdefaultencoding('utf-8') #顯示當前默認編碼方式 print sys.getdefaultencoding()
對應源碼
? ? ? ? 其中對應的百度百科InfoBox源代碼如下圖,代碼中基礎知識可以參考我前面的博文或我的Python爬蟲專利,Selenium不僅僅擅長做自動測試,同樣適合做簡單的爬蟲。
編碼問題
? ? ? ? 此時你仍然可能遇到“'ascii' codec can't encode characters”編碼問題。
? ? ? ?它是因為你創建txt文件時默認是ascii格式,此時你的文字確實'utf-8'格式,所以需要轉換通過如下方法。
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/ )
總結
以上是生活随笔為你收集整理的[Python爬虫] Selenium获取百度百科旅游景点的InfoBox消息盒的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [Python爬虫] Selenium+
- 下一篇: [LeetCode] Length of