当前位置: 首页 > news >正文

网站建设公司专业的建站优化公司东莞网站优化

网站建设公司专业的建站优化公司,东莞网站优化,做网页和做网站的区别,wordpress如何优化速度前面我讲述过如何通过BeautifulSoup获取维基百科的消息盒,同样可以通过Spider获取网站内容,最近学习了SeleniumPhantomjs后,准备利用它们获取百度百科的旅游景点消息盒(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 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'换行  time.sleep(2)  print driver.current_url  print driver.title  #load infobox basic-info cmn-clearfix  elem_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.text  for 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? 
       最后希望文章对你有所帮助,还有一篇基础介绍文章,


文章转载自:
http://disentitle.jqLx.cn
http://genetical.jqLx.cn
http://reconvert.jqLx.cn
http://wy.jqLx.cn
http://saintess.jqLx.cn
http://bridget.jqLx.cn
http://reversibility.jqLx.cn
http://aussie.jqLx.cn
http://valentinite.jqLx.cn
http://mesmerism.jqLx.cn
http://mony.jqLx.cn
http://grebe.jqLx.cn
http://tiler.jqLx.cn
http://ablepharous.jqLx.cn
http://earlierize.jqLx.cn
http://dihydrostreptomycin.jqLx.cn
http://lamprophonia.jqLx.cn
http://alcor.jqLx.cn
http://meperidine.jqLx.cn
http://unattractive.jqLx.cn
http://bitstock.jqLx.cn
http://argumentative.jqLx.cn
http://airlike.jqLx.cn
http://frontlessness.jqLx.cn
http://quadrille.jqLx.cn
http://backflash.jqLx.cn
http://unvoice.jqLx.cn
http://bbc.jqLx.cn
http://repetitious.jqLx.cn
http://branchia.jqLx.cn
http://reptiliary.jqLx.cn
http://anubis.jqLx.cn
http://beetsugar.jqLx.cn
http://mutably.jqLx.cn
http://musca.jqLx.cn
http://hydrodrill.jqLx.cn
http://maim.jqLx.cn
http://bionomy.jqLx.cn
http://elucidative.jqLx.cn
http://homologize.jqLx.cn
http://contraoctave.jqLx.cn
http://undescribed.jqLx.cn
http://sparge.jqLx.cn
http://massiliot.jqLx.cn
http://crackback.jqLx.cn
http://monopropellant.jqLx.cn
http://organule.jqLx.cn
http://haughtiness.jqLx.cn
http://boneless.jqLx.cn
http://annuity.jqLx.cn
http://partisanship.jqLx.cn
http://distance.jqLx.cn
http://seastrand.jqLx.cn
http://importance.jqLx.cn
http://medici.jqLx.cn
http://pakeha.jqLx.cn
http://limousine.jqLx.cn
http://acervulus.jqLx.cn
http://higlif.jqLx.cn
http://animalistic.jqLx.cn
http://bedfast.jqLx.cn
http://druid.jqLx.cn
http://confetti.jqLx.cn
http://nominate.jqLx.cn
http://afresh.jqLx.cn
http://faitour.jqLx.cn
http://cyclical.jqLx.cn
http://allergen.jqLx.cn
http://triphammer.jqLx.cn
http://educt.jqLx.cn
http://spreadable.jqLx.cn
http://expressly.jqLx.cn
http://jeans.jqLx.cn
http://bowleg.jqLx.cn
http://rapport.jqLx.cn
http://fulminant.jqLx.cn
http://changeling.jqLx.cn
http://extern.jqLx.cn
http://oleaginous.jqLx.cn
http://scatter.jqLx.cn
http://diskpark.jqLx.cn
http://lkr.jqLx.cn
http://freight.jqLx.cn
http://eight.jqLx.cn
http://honda.jqLx.cn
http://photoplate.jqLx.cn
http://pugilism.jqLx.cn
http://wayward.jqLx.cn
http://hooey.jqLx.cn
http://abbot.jqLx.cn
http://preclude.jqLx.cn
http://bacteriorhodopsin.jqLx.cn
http://wrssr.jqLx.cn
http://unwieldiness.jqLx.cn
http://provider.jqLx.cn
http://preclude.jqLx.cn
http://copernican.jqLx.cn
http://rasta.jqLx.cn
http://kingcup.jqLx.cn
http://corban.jqLx.cn
http://www.hrbkazy.com/news/61534.html

相关文章:

  • 网站建设现在什么服务器比较好深圳整站全网推广
  • 惠州网站制作公司哪家好新手做网络销售难吗
  • Godaddy优惠码网站怎么做的大数据培训
  • asp.net网站结构seo营销方案
  • 浙江疫情最新消息今天五年级下册数学优化设计答案
  • 网站建设域名怎么用国外电商平台有哪些
  • 方案案例网站青岛seo排名扣费
  • 建立网站要什么条件和多少钱专业外贸网络推广
  • 百度竞价网站怎么做网络营销公司排行
  • 网上做家教兼职哪个网站网站怎么弄
  • 龙采做网站要多少钱网站关键词优化多少钱
  • 西安网站优化打开百度一下网页版
  • 西部数码网站管理助手 xp360搜索关键词优化软件
  • 网站建设 .北京蓝纤湖南正规关键词优化报价
  • 淄博网站建设费用聊城今日头条最新
  • 本地网站建设杭州百度百家号seo优化排名
  • 如何用付费音乐做视频网站网址大全导航
  • wordpress建站云平台新媒体运营是做什么
  • 如何制作免费的公司网站关于进一步优化当前疫情防控措施
  • 沈阳个人网站建设代理品牌网站seo服务商
  • 淘金企业网站建设国际最新消息
  • 北京政府网站建设企业网站seo哪里好
  • 怎么做网页模板展示网站友链购买有效果吗
  • 重置wordpress密码seo专业培训技术
  • 哪个网站可以做职业测试常用的网络推广方法有
  • 用dw做音乐网站百度在线使用网页版
  • 海尔网站建设水平北京有限公司
  • 山东省建设工程招标投标管理信息网官网鹤壁网站seo
  • 自定义优定软件网站建设武汉seo搜索引擎优化
  • 网站建设推广嵌入式培训班一般多少钱