当前访客身份:游客 [ 登录 | 申请入伍 ]

江西高安瓷砖厂分布图|经验教程

当前位置:
江西快3开奖走势图 » 后援区 » 经验教程 » 主题内容
查看: 842|回复: 0
打印 上一主题 下一主题
收起左侧

如何利用Python脚本生成sitemap.xml-网站建设

[复制链接]

144

主题

144

帖子

90

积分

初入站场

Rank: 1

积分
90
跳转到指定楼层
楼主
佚名 发表于 2017-4-16 00:00:00 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本文来源:http://www.piucs.com/a/www.hbca.org.cn/

江西快3开奖走势图 www.piucs.com,与其说这种自营模式是一种互联网平台,不如说他们是在借助互联网的渠道、技术等来为线下门店提供更好的用户服务。Baidu'sChineseinternetgiantBaiduhasannouncedastrategicpartnershipwithautomobilemanufacturersBAICMotortoexpandthesmartvehiclesector.Baidu'sautonomousdrivingresearchanddevelopmentarm,BaiduIntelligentVehicle,willworkwithBAIContwokeyprojects.TheyplantolaunchacarmodeldrivenbyBaidu'stelematicssolutionsinthefirsthalfofthisyearandroadtesttheBAIC'slimitedself-drivingcarsbytheendofthisyear.BaidupresidentZhangYaqinsaidintelligentvehicletechnologyisanincreasinglyimportantareaforBaidu."WearedelightedtobepartneringwithBAIC,oneofthemostactiveautomobilemanufacturersworldwideinpromotingsmartsoftware,"hesaid."Welookforwardtoworkingtogethertoestablishasmartvehicleecosystemtosupportthisimportantindustrytrend."Undertheagreement,thecompanieswillalsocooperateonlevel3autonomousdrivingtechnologyincludinghighdefinitionmaps,whileBaiduwillprovideBAICwithintelligentvehicleoriginalequipmentmanufacturer(OEM)solutionsincludingCarLife,CoDriver,andMapAuto.BaiduandBAICwillalsolauncharesearchlabtopromotetechnologicalcollaborationandthemassapplicationofintelligentvehicletechnology.TheCarLife,asmartphoneintegrationapplicationforconnectedcars,hasbeendeployedinnearly150carmodelsfrom60automobilemanufacturers,includingAudi,Mercedes-Benz,HyundaiandShanghaiGM(includingChevrolet,BuickandCadillacbrands),andcountsover700,000carownersasactiveusers.TheannouncementaboutthepartnershipwasmadeduringtheannualInternationalConsumerElectronicsShow.Itwasn'ttheonlycollaborationannoundedattheshow.NissanunveiledplanstointegrateMicrosoft'sdigitalassistantintosomeofitsvehiclesandFordhasbecomethefirstautomakertobringAmazonEcho,avoicecontrollerthatusesartificialintelligence,intotheircars.

安装lxml
首先需要pip install lxml安装lxml库。
如果你在ubuntu上遇到了以下错误:
#include "libxml/xmlversion.h"
compilation terminated.
error: command 'x86_64-linux-gnu-gcc' failed with exit status 1
----------------------------------------
Cleaning up...
Removing temporary dir /tmp/pip_build_root...
Command /usr/bin/python -c "import setuptools, tokenize;__file__='/tmp/pip_build_root/lxml/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /tmp/pip-O4cIn6-record/install-record.txt --single-version-externally-managed --compile failed with error code 1 in /tmp/pip_build_root/lxml
Exception information:
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/pip/basecommand.py", line 122, in main
status = self.run(options, args)
File "/usr/lib/python2.7/dist-packages/pip/commands/install.py", line 283, in run
requirement_set.install(install_options, global_options, root=options.root_path)
File "/usr/lib/python2.7/dist-packages/pip/req.py", line 1435, in install
requirement.install(install_options, global_options, *args, **kwargs)
File "/usr/lib/python2.7/dist-packages/pip/req.py", line 706, in install
cwd=self.source_dir, filter_stdout=self._filter_install, show_stdout=False)
File "/usr/lib/python2.7/dist-packages/pip/util.py", line 697, in call_subprocess
% (command_desc, proc.returncode, cwd))
InstallationError: Command /usr/bin/python -c "import setuptools, tokenize;__file__='/tmp/pip_build_root/lxml/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /tmp/pip-O4cIn6-record/install-record.txt --single-version-externally-managed --compile failed with error code 1 in /tmp/pip_build_root/lxml
请安装以下依赖:
sudo apt-get install libxml2-dev libxslt1-dev
Python代码
下面是生成sitemap和sitemapindex索引的代码,可以按照需求传入需要的参数,或者增加字段:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import io
import re
from lxml import etree
def generate_xml(filename, url_list):
"""Generate a new xml file use url_list"""
root = etree.Element('urlset',
       xmlns="http://www.sitemaps.org/schemas/sitemap/0.9")
for each in url_list:
  url = etree.Element('url')
  loc = etree.Element('loc')
  loc.text = each
  url.append(loc)
  root.append(url)
header = u'\n'
s = etree.tostring(root, encoding='utf-8', pretty_print=True)
with io.open(filename, 'w', encoding='utf-8') as f:
  f.write(unicode(header+s))
def update_xml(filename, url_list):
"""Add new url_list to origin xml file."""
f = open(filename, 'r')
lines = [i.strip() for i in f.readlines()]
f.close()
old_url_list = []
for each_line in lines:
  d = re.findall('(http:\/\/.+)', each_line)
  old_url_list += d
url_list += old_url_list
generate_xml(filename, url_list)
def generatr_xml_index(filename, sitemap_list, lastmod_list):
"""Generate sitemap index xml file."""
root = etree.Element('sitemapindex',
       xmlns="http://www.sitemaps.org/schemas/sitemap/0.9")
for each_sitemap, each_lastmod in zip(sitemap_list, lastmod_list):
  sitemap = etree.Element('sitemap')
  loc = etree.Element('loc')
  loc.text = each_sitemap
  lastmod = etree.Element('lastmod')
  lastmod.text = each_lastmod
  sitemap.append(loc)
  sitemap.append(lastmod)
  root.append(sitemap)
header = u'\n'
s = etree.tostring(root, encoding='utf-8', pretty_print=True)
with io.open(filename, 'w', encoding='utf-8') as f:
  f.write(unicode(header+s))
if __name__ == '__main__':
urls = ['http://www.baidu.com'] * 10
mods = ['2004-10-01T18:23:17+00:00'] * 10
generatr_xml_index('index.xml', urls, mods)
效果
生成的效果应该是这种格式:
sitemap格式:
xml version="1.0" encoding="UTF-8"?>
xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">

http://www.example.com/foo.html

sitemapindex格式:
xml version="1.0" encoding="UTF-8"?>
  xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">

  http://www.example.com/sitemap1.xml.gz
  2004-10-01T18:23:17+00:00

[1]
[2]  下一页

本版积分规则

站友网手机客户端: 江西快3开奖走势图Android iPhone WP7 QQ

© 站友网(江西快3开奖走势图 www.piucs.com)|关于我们|小黑屋| 闽ICP备09072999号  

快速回复 返回顶部 返回列表
安徽快3和值 湖北快3一定牛预测 北京快3技巧 吉林快3开奖走势图 中囯福利彩票甘肃快3 上海快3开奖结果一定牛
安徽快3专家预测 内蒙古快3推荐号 河南快3走势图 福建快3走势图福建快3基本走势图 内蒙古快3遗漏统计360 吉林快3和值走势
河北快3推荐和值 福建快3开奖结果明天 上海快3走势 河南快3今天开奖走势图 北京快3开奖结果走势图今天 吉林快3第三位遗漏值尾走势图
北京赛车pk10现场 pk10计划软件 安徽11选5开奖 解密三尾中特 甘肃11选5玩法
江西时时彩购买 北京赛车pk10官网 体彩12091期浙江6加1 香港白小姐中特网站 浙江11选5开奖直播