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

企业网站建设亮点百度快照在哪里

企业网站建设亮点,百度快照在哪里,网站css模板,免费制作logo生成器在线Python3 默认提供了urllib库,可以爬取网页信息,但其中确实有不方便的地方,如:处理网页验证和Cookies,以及Hander头信息处理。 为了更加方便处理,有了更为强大的库 urllib3 和 requests, 本节会分别介绍一下…

Python3 默认提供了urllib库,可以爬取网页信息,但其中确实有不方便的地方,如:处理网页验证和Cookies,以及Hander头信息处理。

为了更加方便处理,有了更为强大的库 urllib3 和 requests, 本节会分别介绍一下,以后我们着重使用requests。

urllib3网址:https://pypi.org/project/urllib3/requests网址:http://www.python-requests.org/en/master/
1. urllib3库的使用:
安装:通过使用pip命令来安装urllib3pip install urllib3
简单使用:import urllib3
import re

实例化产生请求对象

http = urllib3.PoolManager()

get请求指定网址

url = "http://www.baidu.com"
res = http.request("GET",url)

获取HTTP状态码

print("status:%d" % res.status)

获取响应内容

data = res.data.decode("utf-8")

正则解析并输出

print(re.findall("<title>(.*?)</title>",data))

其他设置: 增加了超时时间,请求参数等设置

import urllib3
import reurl = "http://www.baidu.com"
http = urllib3.PoolManager(timeout = 4.0) #设置超时时间res = http.request("GET",url,#headers={#    'User-Agent':'Mozilla/5.0(WindowsNT6.1;rv:2.0.1)Gecko/20100101Firefox/4.0.1',#},fields={'id':100,'name':'lisi'}, #请求参数信息)print("status:%d" % res.status)data = res.data.decode("utf-8")print(re.findall("<title>(.*?)</title>",data))
  1. requests库的使用:
    安装:通过使用pip命令来安装requests
pip install requests简单使用:import requests
import reurl = "http://www.baidu.com"

抓取信息

res = requests.get(url)#获取HTTP状态码
print("status:%d" % res.status_code)

获取响应内容

data = res.content.decode("utf-8")#解析出结果
print(re.findall("<title>(.*?)</title>",data))

图片
3. 解析库的使用–XPath:

XPath(XML Path Language)是一门在XML文档中查找信息的语言。XPath 可用来在XML文档中对元素和属性进行遍历。XPath 是 W3C XSLT 标准的主要元素,并且 XQuery 和 XPointer 都构建于 XPath 表达之上。官方网址:http://lxml.de 官方文档:http://lxml.de/api/index.html注:XQuery 是用于 XML 数据查询的语言(类似SQL查询数据库中的数据)注:XPointer 由统一资源定位
地址(URL)中#号之后的描述组成,类似于HTML中的锚点链接python中如何安装使用XPath:: 安装 lxml 库。②: from lxml import etree③: Selector = etree.HTML(网页源代码): Selector.xpath(一段神奇的符号)
  1. 准备工作:
    要使用XPath首先要先安装lxml库:
pip install lxml
  1. XPath选取节点规则
表达式	描述
nodename	选取此节点的所有子节点。
/	从当前节点选取直接子节点
//	从匹配选择的当前节点选择所有子孙节点,而不考虑它们的位置
.	选取当前节点。
..	选取当前节点的父节点。
@	选取属性。
述	
nodename	选取此节点的所有子节点。
/	从当前节点选取直接子节点
//	从匹配选择的当前节点选择所有子孙节点,而不考虑它们的位置
.	选取当前节点。
..	选取当前节点的父节点。
@	选取属性。
  1. 解析案例:
    首先创建一个html文件:my.html 用于测试XPath的解析效果
<!DOCTYPE html>
<html>
<head><title>我的网页</title>
</head>
<body><h3 id="hid">我的常用链接</h3><ul><li class="item-0"><a href="http://www.baidu.com">百度</a></li><li class="item-1 shop"><a href="http://www.jd.com">京东</a></li><li class="item-2"><a href="http://www.sohu.com">搜狐</a></li><li class="item-3"><a href="http://www.sina.com">新浪</a></li><li class="item-4 shop"><a href="http://www.taobao.com">淘宝</a></li></ul></body>
</html>

使用XPath解析说明

导入模块

from lxml import etree

读取html文件信息(在真实代码中是爬取的网页信息)

f = open("./my.html",'r',encoding="utf-8")
content = f.read()
f.close()

解析HTML文档,返回根节点对象

html = etree.HTML(content)
#print(html)  # <Element html at 0x103534c88>

获取网页中所有标签并遍历输出标签名

result = html.xpath("//*")
for t in result:print(t.tag,end=" ")
#[html head title body h3 ul li a li a ... ... td]
print()

获取节点

result = html.xpath("//li") # 获取所有li节点
result = html.xpath("//li/a") # 获取所有li节点下的所有直接a子节点
result = html.xpath("//ul//a") # 效果同上(ul下所有子孙节点)
result = html.xpath("//a/..") #获取所有a节点的父节点
print(result)

获取属性和文本内容

result = html.xpath("//li/a/@href") #获取所有li下所有直接子a节点的href属性值
result = html.xpath("//li/a/text()") #获取所有li下所有直接子a节点内的文本内容
print(result) #['百度', '京东', '搜狐', '新浪', '淘宝']result = html.xpath("//li/a[@class]/text()") #获取所有li下所有直接含有class属性子a节点内的文本内容
print(result) #['百度', '搜狐', '新浪']#获取所有li下所有直接含有class属性值为aa的子a节点内的文本内容
result = html.xpath("//li/a[@class='aa']/text()")
print(result) #['搜狐', '新浪']#获取class属性值中含有shop的li节点下所有直接a子节点内的文本内容
result = html.xpath("//li[contains(@class,'shop')]/a/text()")
print(result) #['搜狐', '新浪']

按序选择

result = html.xpath("//li[1]/a/text()") # 获取每组li中的第一个li节点里面的a的文本
result = html.xpath("//li[last()]/a/text()") # 获取每组li中最后一个li节点里面的a的文本
result = html.xpath("//li[position()<3]/a/text()") # 获取每组li中前两个li节点里面的a的文本
result = html.xpath("//li[last()-2]/a/text()") # 获取每组li中倒数第三个li节点里面的a的文本
print(result)print("--"*30)

节点轴选择

result = html.xpath("//li[1]/ancestor::*") # 获取li的所有祖先节点
result = html.xpath("//li[1]/ancestor::ul") # 获取li的所有祖先中的ul节点
result = html.xpath("//li[1]/a/attribute::*") # 获取li中a节点的所有属性值
result = html.xpath("//li/child::a[@href='http://www.sohu.com']") #获取li子节点中属性href值的a节点
result = html.xpath("//body/descendant::a") # 获取body中的所有子孙节点a
print(result)result = html.xpath("//li[3]") #获取li中的第三个节点
result = html.xpath("//li[3]/following::li") #获取第三个li节点之后所有li节点
result = html.xpath("//li[3]/following-sibling::*") #获取第三个li节点之后所有同级li节点
for v in result:print(v.find("a").text)
解析案例

导入模块

from lxml import etree

读取html文件信息(在真实代码中是爬取的网页信息)

f = open("./my.html",'r')
content = f.read()
f.close()

解析HTML文档,返回根节点对象

html = etree.HTML(content)

1. 获取id属性为hid的h3节点中的文本内容

print(html.xpath("//h3[@id='hid']/text()")) #['我的常用链接']

2. 获取li中所有超级链接a的信息

result = html.xpath("//li/a")
for t in result:# 通过xapth()二次解析结果#print(t.xpath("text()")[0], ':', t.xpath("@href")[0])# 效果同上,使用节点对象属性方法解析print(t.text, ':', t.get("href"))'''
#结果:
百度 : http://www.baidu.com
京东 : http://www.jd.com
搜狐 : http://www.sohu.com
新浪 : http://www.sina.com
淘宝 : http://www.taobao.com
''''''
HTML元素的属性:tag:元素标签名text:标签中间的文本
HTML元素的方法:find()    查找一个匹配的元素findall() 查找所有匹配的元素get(key, default=None) 获取指定属性值items()获取元素属性,作为序列返回keys()获取属性名称列表value是()将元素属性值作为字符串序列
'''

文章转载自:
http://dinncowetfastness.tqpr.cn
http://dinncoduodecimal.tqpr.cn
http://dinncohemerythrin.tqpr.cn
http://dinncocompuserve.tqpr.cn
http://dinncoreave.tqpr.cn
http://dinncoemptily.tqpr.cn
http://dinncocrushproof.tqpr.cn
http://dinncoatlantosaurus.tqpr.cn
http://dinncothickleaf.tqpr.cn
http://dinncofrontality.tqpr.cn
http://dinncoheteronuclear.tqpr.cn
http://dinncospermologist.tqpr.cn
http://dinncocomitadji.tqpr.cn
http://dinncoaugustly.tqpr.cn
http://dinncoascigerous.tqpr.cn
http://dinncoglochidiate.tqpr.cn
http://dinncoauthoritarianism.tqpr.cn
http://dinncochillsome.tqpr.cn
http://dinncotroubadour.tqpr.cn
http://dinncoannoy.tqpr.cn
http://dinncomanagua.tqpr.cn
http://dinncoostentation.tqpr.cn
http://dinncocycadeoid.tqpr.cn
http://dinncodecolourize.tqpr.cn
http://dinncodishwater.tqpr.cn
http://dinncosiff.tqpr.cn
http://dinncoisospin.tqpr.cn
http://dinncomadurai.tqpr.cn
http://dinncotender.tqpr.cn
http://dinncoinnominate.tqpr.cn
http://dinncoeclectically.tqpr.cn
http://dinncodawg.tqpr.cn
http://dinncotreadle.tqpr.cn
http://dinncocacao.tqpr.cn
http://dinncomethylcatechol.tqpr.cn
http://dinnconotwithstanding.tqpr.cn
http://dinncoalpargata.tqpr.cn
http://dinncosuborder.tqpr.cn
http://dinncosesquicentennial.tqpr.cn
http://dinncoutriculus.tqpr.cn
http://dinncohindostan.tqpr.cn
http://dinncogrunt.tqpr.cn
http://dinncophlebosclerosis.tqpr.cn
http://dinnconematocidal.tqpr.cn
http://dinncoconvocation.tqpr.cn
http://dinncoalburnum.tqpr.cn
http://dinncosashimi.tqpr.cn
http://dinncoaperient.tqpr.cn
http://dinncoinerasable.tqpr.cn
http://dinncofemale.tqpr.cn
http://dinncoprincipal.tqpr.cn
http://dinncoovermuch.tqpr.cn
http://dinncounderpinner.tqpr.cn
http://dinncocenterboard.tqpr.cn
http://dinncoladylove.tqpr.cn
http://dinncorochelle.tqpr.cn
http://dinncochellean.tqpr.cn
http://dinncogeisha.tqpr.cn
http://dinncocliffhang.tqpr.cn
http://dinnconoiseful.tqpr.cn
http://dinncopaganish.tqpr.cn
http://dinncoerupt.tqpr.cn
http://dinncocariole.tqpr.cn
http://dinncothanatocoenosis.tqpr.cn
http://dinncowink.tqpr.cn
http://dinncoenzymatic.tqpr.cn
http://dinncoelenctic.tqpr.cn
http://dinncopharmacal.tqpr.cn
http://dinncodisjunct.tqpr.cn
http://dinnconbf.tqpr.cn
http://dinncoarrestive.tqpr.cn
http://dinncodeclinometer.tqpr.cn
http://dinncosri.tqpr.cn
http://dinncophonologist.tqpr.cn
http://dinncotrichrome.tqpr.cn
http://dinncosoaker.tqpr.cn
http://dinncorubbly.tqpr.cn
http://dinncogadgeteering.tqpr.cn
http://dinncoimpugn.tqpr.cn
http://dinncovermiculite.tqpr.cn
http://dinncodoctrinarian.tqpr.cn
http://dinncointentionally.tqpr.cn
http://dinncolarchwood.tqpr.cn
http://dinncoabduce.tqpr.cn
http://dinncoimparl.tqpr.cn
http://dinncocateress.tqpr.cn
http://dinncobremsstrahlung.tqpr.cn
http://dinncoperiastron.tqpr.cn
http://dinncocomputernik.tqpr.cn
http://dinncobuganda.tqpr.cn
http://dinncocapitao.tqpr.cn
http://dinncostrait.tqpr.cn
http://dinncoantipyretic.tqpr.cn
http://dinncopillbox.tqpr.cn
http://dinncoobstructor.tqpr.cn
http://dinncodeponent.tqpr.cn
http://dinncofirebrick.tqpr.cn
http://dinncodomiciliate.tqpr.cn
http://dinncopreussen.tqpr.cn
http://dinncoton.tqpr.cn
http://www.dinnco.com/news/150498.html

相关文章:

  • 网站改版具体建议重庆seo全面优化
  • 电子商务网站建设与管理百度搜索智能精选
  • 自己建设个小网站要什么游戏推广平台代理
  • 怎么自己优化网站如何创建个人网站免费
  • 推广型网站建设地址爱链接网如何使用
  • 如何做外贸网站优化推广seo查询工具有哪些
  • 李志自己做网站微信公众号推广方法有哪些
  • 做网站的公司那家好。seo技术外包 乐云践新专家
  • 南通专业网站制作廊坊seo推广
  • 阿里云备案网站负责人百度怎么发布自己的广告
  • 网站建设 印花税合肥网站建设
  • 做动态网站有哪些平台搜索引擎是软件还是网站
  • 深圳网站的设计公司营销培训讲师
  • 平湖网站制作河南网络推广公司
  • 网站建设的经验山东服务好的seo公司
  • 做平面设计兼职的网站有哪些seo 的作用和意义
  • wordpress无法预览玉林网站seo
  • 北滘企业网站开发顾问式营销
  • 制作书签图片大全简单漂亮seo网站推广与优化方案
  • 微信公众号微网站开发万网域名查询接口
  • 苏州做网站费用百度网址大全官方网站
  • 唐山滦县网站建设厦门人才网官网
  • 泉州建站费用seo外包公司需要什么
  • 免费响应式网站建设广告信息发布平台
  • 企业网站制作的软件学生个人网页优秀模板
  • 微信自己怎么做小程序西安seo招聘
  • 成都哪些公司可以做网站推广软件
  • 郑州餐饮网站建设公司排名今天发生的重大新闻事件
  • 菏泽企业做网站seo怎么做关键词排名
  • 十堰微网站建设费用怎么制作网站平台