python指定url

Python019

python指定url,第1张

今天简单使用了一下python的re模块和lxml模块,分别利用的它们提供的正则表达式和xpath来解析页面源码从中提取所需的title,xpath在完成这样的小任务上效率非常好,在这里之所以又使用了一下正则表达式是因为xpath在处理一些特殊的页面的时候会出现乱码的情况,当然这不是xpath的原因,而是页面本身编码,跟utf-8转码之间有冲突所致,这里看代码:

python抽取指定url页面的title方法(python获取当前页面的url) python 抽取 url title 脚本之家 第1张

# !/usr/bin/python

#-*-coding:utf-8-*-

'''

功能:抽取指定url的页面内容中的title

'''

import re

import chardet

import urllib

from lxml import etree

def utf8_transfer(strs):

'''

utf8编码转换

'''

try:

if isinstance(strs, unicode):

strs = strs.encode('utf-8')

elif chardet.detect(strs)['encoding'] == 'GB2312':

strs = strs.decode("gb2312", 'ignore').encode('utf-8')

elif chardet.detect(strs)['encoding'] == 'utf-8':

strs = strs.decode('utf-8', 'ignore').encode('utf-8')

except Exception, e:

print 'utf8_transfer error', strs, e

return strs

def get_title_xpath(Html):

'''

用xpath抽取网页Title

'''

Html = utf8_transfer(Html)

Html_encoding = chardet.detect(Html)['encoding']

page = etree.HTML(Html, parser=etree.HTMLParser(encoding=Html_encoding

完整的url语法格式:

协议://用户名@密码:子域名.域名.顶级域名:端口号/目录/文件名.文件后缀?参数=值#标识

2 、urlparse模块对url的处理方法

urlparse模块对url的主要处理方法有:urljoin/urlsplit/urlunsplit/urlparse等。该模块对url的定义采用六元组的形式:schema://netloc/pathparameters?query#fragment。其中,netloc包含下表的后4个属性

urlparse()

利用urlparse()方法对url进行解析,返回六元组;urlunparse()对六元组进行组合

urljoin()

利用urljoin()方法对绝对url地址与相对url地址进行拼合

       主要使用urljoin()比较常用——给出以下示例:   

>>>from urllib.parse import urljoin

>>>urljoin("http://www.chachabei.com/folder/currentpage.html", "anotherpage.html")

'http://www.chachabei.com/folder/anotherpage.html'

>>>urljoin("http://www.chachabei.com/folder/currentpage.html", "/anotherpage.html")

'http://www.chachabei.com/anotherpage.html'

>>>urljoin("http://www.chachabei.com/folder/currentpage.html", "folder2/anotherpage.html")

'http://www.chachabei.com/folder/folder2/anotherpage.html'

>>>urljoin("http://www.chachabei.com/folder/currentpage.html", "/folder2/anotherpage.html")

'http://www.chachabei.com/folder2/anotherpage.html'

>>>urljoin("http://www.chachabei.com/abc/folder/currentpage.html", "/folder2/anotherpage.html")

'http://www.chachabei.com/folder2/anotherpage.html'

>>>urljoin("http://www.chachabei.com/abc/folder/currentpage.html", "../anotherpage.html")

'http://www.chachabei.com/abc/anotherpage.html'

登录后复制

urlsplit()

利用urlsplit()方法可以对URL进行分解;与urlparse()相比,urlsplit()函数返回一个五元组,没有parameter参数。

相应的,urlunsplit()方法可以对urlsplit()分解的五元组进行合并。两种方法组合在一起,可以对URL进行有效地格式化,特殊字符在此过程中得到转换。

3 urllib模块对url的编码与解码

urllib模块的quote_plus()方法实现对url的编码,包括对中文的编码;unquote_plus()方法实现对url的解码,包括对中文的解码。

Python

urllib

urljoin()

名表折扣店瑞士手表

精选推荐

广告

python url 参数修改方法

0下载·0评论

2021年1月21日

Python 中的 urlencode 和 urldecode 操作

4859阅读·0评论·2点赞

2019年1月2日

Python常用函数(urlencode 与 urldecode)

496阅读·0评论·0点赞

2022年12月10日

python爬虫URL重试机制的实现方法(python2.7以及python3.5)

0下载·0评论

2020年12月25日

python—接口调用

1.0W阅读·0评论·6点赞

2020年1月18日

python调用第三方接口获取数据_python 接口实现 供第三方调用的例子

116阅读·0评论·0点赞

2020年11月28日

Python中url的编码以及解码

8828阅读·0评论·3点赞

2021年9月7日

使用python 实现url 接口的方法

3334阅读·0评论·2点赞

2019年1月22日

Python2.7 网络请求 urllib、urllib2和requests

777阅读·0评论·0点赞

2022年6月21日

python 中的 urlencode 编码与 urldecode 解码

3732阅读·1评论·1点赞

2022年3月10日

python2.7 模拟登录后调用自定义HTTP接口发送告警

113阅读·0评论·0点赞

2020年8月4日

python2.7版本登录获取cookie并调用接口(二)

804阅读·0评论·0点赞

2022年3月8日

python调用接口限流_Kong 网关API安装部署以及应用实例----------腾云驾雾

418阅读·0评论·0点赞

2020年12月5日

urllib2模块中文翻译与学习 - Python 2.7.8官方文档

44阅读·0评论·0点赞

2014年10月30日

Python 2.7 获取网络信息(Urllib)

3262阅读·0评论·1点赞

2017年6月30日

Python---关于URL的处理(基于Python2.7版本)

491阅读·0评论·0点赞

2019年6月3日

python爬虫开发 urlparse、parse_qs、urlencode、quote、unquote、urljoin 用法总结

284阅读·0评论·0点赞

2022年10月11日

Python 解析获取 URL 参数以及使用

3806阅读·0评论·3点赞

2022年6月9日

Python-URL编码和URL解码方法

8796阅读·2评论·3点赞

2020年8月13日

去首页

看看更多热门内容