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

网站评论管理怎么做的成都到西安距离

网站评论管理怎么做的,成都到西安距离,洛阳哪有做公司网站的,凤岗本地网站前面我讲述过如何通过BeautifulSoup获取维基百科的消息盒#xff0c;同样可以通过Spider获取网站内容#xff0c;最近学习了SeleniumPhantomjs后#xff0c;准备利用它们获取百度百科的旅游景点消息盒#xff08;InfoBox#xff09;#xff0c;这也是毕业设计实体对齐和属… 前面我讲述过如何通过BeautifulSoup获取维基百科的消息盒同样可以通过Spider获取网站内容最近学习了SeleniumPhantomjs后准备利用它们获取百度百科的旅游景点消息盒InfoBox这也是毕业设计实体对齐和属性的对齐的语料库前期准备工作。希望文章对你有所帮助~ 源代码 # codingutf-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_pathG:\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[idsearchForm]/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[classbasic-info cmn-clearfix]/dl/dt)    elem_value  driver.find_elements_by_xpath(//div[classbasic-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 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”编码问题。 它是因为你创建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://www.yutouwan.com/news/437015/

相关文章:

  • 临清网站建设公司免费字体设计网站
  • 网页设计网站值得推荐网站内地图位置怎么做
  • 做百度推广需要自己有个网站吗鹤山网站建设
  • 建设网站的申请信用卡分期付款软件开发岗位介绍
  • 怎么编辑网站后台怎样弄网站
  • 可以做任务的网站有哪些视频网站 flash h5
  • 网站admin密码阿里巴巴网页版入口
  • 网站建设找盖亚科技电商平台排名100强
  • 网站建设价格情况科技成果展示网站建设方案
  • 网站建设流程html做网站有哪些要求
  • 网站充值接口网站seo外链
  • 网站域名到期后果做网站参考文献
  • 新网 网站建立手机建立一个免费网站
  • 织梦如何做几种语言的网站门户网站风格
  • 网页设计与制作教材电子版嘉定区整站seo十大排名
  • 网站域名区别吗济南智能网站建设哪家好
  • 珠海科技网站建设制做网站首先应该怎么做
  • 义乌微信网站建设费用企业网站模板官网
  • 前端一般模仿什么网站网站建设的法律问题
  • app免费制作网站模板免费门户网站制作
  • 设计外贸英文网站湖南刚刚发生的大事件
  • 做网站销售的技巧建筑网片厂家
  • 高端响应式网站开发安徽服饰网站建设
  • 中煤地建设有限公司网站软件平台架构
  • 网站建设 开发工具 python挂机宝如何做网站
  • 怎么在虚拟空间做两个网站做公益网站需要什么资质
  • 网站开发需要提供哪些资料wordpress主题 洛米
  • 网站制作与app开发哪个要难一点策划公司网站
  • vue做的网站有什么wordpress海报功能
  • 网页设计实训报告保利集团吐鲁番seo快速排名