1. 首先通过pip install builtwith安装builtwith

C:\Users\Administrator>pip install builtwith
Collecting builtwith
Downloading builtwith-1.3.2.tar.gz
Installing collected packages: builtwith
Running setup.py install for builtwith ... done
Successfully installed builtwith-1.3.2

2. 在pycharm中新建工程并输入下面测试代码

import builtwith
tech_used = builtwith.parse('http://www.baidu.com')
print(tech_used)

运行会得到下面的错误:

C:\Users\Administrator\AppData\Local\Programs\Python\Python36\python.exe F:/python/first/FirstPy
Traceback (most recent call last):
File "F:/python/first/FirstPy", line 1, in <module>
import builtwith
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-packages\builtwith\__init__.py", line 43
except Exception, e:
^
SyntaxError: invalid syntax Process finished with exit code 1

原因是builtwith是基于2.x版本的,需要修改几个地方,在pycharm出错信息中双击出错文件,进行修改,主要修改下面三种:
1. Python2中的 “Exception ,e”的写法已经不支持,需要修改为“Exception as e”。
2. Python2中print后的表达式在Python3中都需要用括号括起来。
3. builtwith中使用的是Python2中的urllib2工具包,这个工具包在Python3中是不存在的,需要修改urllib2相关的代码。
1和2容易修改,下面主要针对第3点进行修改:
首先将import urllib2替换为下面的代码:

import urllib.request
import urllib.error

然后将urllib2的相关方法替换如下:

request = urllib.request.Request(url, None, {'User-Agent': user_agent})
response = urllib.request.urlopen(request)

再次运行项目,遇到下面错误:

C:\Users\Administrator\AppData\Local\Programs\Python\Python36\python.exe F:/python/first/FirstPy
Traceback (most recent call last):
File "F:/python/first/FirstPy", line 3, in <module>
builtwith.parse('http://www.baidu.com')
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-packages\builtwith\__init__.py", line 62,
in builtwith
if contains(html, snippet):
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-packages\builtwith\__init__.py", line 105,
in contains
return re.compile(regex.split('\\;')[0], flags=re.IGNORECASE).search(v)
TypeError: cannot use a string pattern on a bytes-like object Process finished with exit code 1

这是因为urllib返回的数据格式已经发生了改变,需要进行转码,将下面的代码:

if html is None:
html = response.read()

修改为

if html is None:
html = response.read()
html = html.decode('utf-8')

再次运行得到最终结果如下:

C:\Users\Administrator\AppData\Local\Programs\Python\Python36\python.exe F:/python/first/FirstPy
{'javascript-frameworks': ['jQuery']} Process finished with exit code 0

但是如果把网站换成 'www.163.com',运行再次报错如下:

C:\Users\Administrator\AppData\Local\Programs\Python\Python36\python.exe F:/python/first/FirstPy
Error: 'utf-8' codec can't decode byte 0xcd in position 500: invalid continuation byte
Traceback (most recent call last):
File "F:/python/first/FirstPy", line 2, in <module>
tech_used = builtwith.parse('http://www.163.com')
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-packages\builtwith\__init__.py", line 63,
in builtwith
if contains(html, snippet):
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-packages\builtwith\__init__.py", line 106,
in contains
return re.compile(regex.split('\\;')[0], flags=re.IGNORECASE).search(v)
TypeError: cannot use a string pattern on a bytes-like object Process finished with exit code 1

似乎还是编码的问题,将编码设置成 ‘GBK’,运行成功如下:

C:\Users\Administrator\AppData\Local\Programs\Python\Python36\python.exe F:/python/first/FirstPy
{'web-servers': ['Nginx']} Process finished with exit code 0

所以不同的网站需要用不同的解码方式么?下面介绍一种判别网站编码格式的方法。
我们需要安装一个叫chardet的工具包,如下:

C:\Users\Administrator>pip install chardet
Collecting chardet
Downloading chardet-2.3.0-py2.py3-none-any.whl (180kB)
100% |████████████████████████████████| 184kB 616kB/s
Installing collected packages: chardet
Successfully installed chardet-2.3.0 C:\Users\Administrator>

将byte数据传入chardet的detect方法后会得到一个Dict,里面有两个值,一个是置信值,一个是编码方式

{'encoding': 'utf-8', 'confidence': 0.99}  

将builtwith对应的代码做下面修改:

encode_type = chardet.detect(html)
if encode_type['encoding'] == 'utf-8':
html = html.decode('utf-8')
else:
html = html.decode('gbk')

记得 import chardet!!!!
加入chardet判断字符编码的方式后,就能适配网站了~~~~

 http://blog.csdn.net/fengzhizi76506/article/details/61617067

python3中使用builtwith的方法(很详细)的更多相关文章

  1. Python3中使用PyMongo的方法详解

    前言 本文主要给大家介绍的是关于在Python3使用PyMongo的方法,分享出来供大家参考学习,下面话不多说了,来一起看看详细介绍: MongoDB存储 在这里我们来看一下Python3下Mongo ...

  2. Python3中BeautifulSoup的使用方法

    BeautifulSoup的使用 我们学习了正则表达式的相关用法,但是一旦正则写的有问题,可能得到的就不是我们想要的结果了,而且对于一个网页来说,都有一定的特殊的结构和层级关系,而且很多标签都有id或 ...

  3. c++中的const用法(很详细)——转

    http://www.cnblogs.com/ymy124/archive/2012/04/16/2451433.html const给人的第一印象就是定义常量. (1)const用于定义常量. 例如 ...

  4. 转载过来的参考内容---常规36个WEB渗透测试漏洞描述及修复方法----很详细

        常规WEB渗透测试漏洞描述及修复 --转自:http://www.51testing.com/html/92/n-3723692.html (1). Apache样例文件泄漏 漏洞描述 apa ...

  5. VS2015ASP.NET MVC5项目中Spring.NET配置方法(超详细)

    首先,在ASP.NET MVC5项目右键,如下图所示,选择“管理Nuget程序包...” 然后,在弹出的页面的搜索框中输入“spring.web”,在返回结果中选择Spring.Web和Spring. ...

  6. Python3中使用urllib的方法详解(header,代理,超时,认证,异常处理)_python

    我们可以利用urllib来抓取远程的数据进行保存哦,以下是python3 抓取网页资源的多种方法,有需要的可以参考借鉴. 1.最简单 import urllib.request response = ...

  7. Python3中使用urllib的方法详解(header,代理,超时,认证,异常处理)

    出自  http://www.jb51.net/article/93125.htm

  8. 【spring data jpa】jpa中使用count计数方法

    spring data jpa中使用count计数方法很简单 直接在dao层写方法即可 int countByUidAndTenementId(String parentUid, String ten ...

  9. [翻译]python3中新的字符串格式化方法-----f-string

    从python3.6开始,引入了新的字符串格式化方式,f-字符串. 这使得格式化字符串变得可读性更高,更简洁,更不容易出现错误而且速度也更快. 在本文后面,会详细介绍f-字符串的用法. 在此之前,让我 ...

随机推荐

  1. PS图层混合算法之五(饱和度,色相,颜色,亮度)

    饱和度模式: HcScYc =HBSAYB 饱和度模式:是采用底色的亮度.色相以及绘图色的饱和度来创建最终色.如果绘图色的饱和度为0,则原图没有变化. 输出图像的饱和度为上层,色调和亮度保持为下层. ...

  2. gtk+程序在关闭主窗口时的事件流

    当鼠标单击gtk+窗口的关闭按钮时,程序首先接收到delete_event,当该事件处理函数返回TRUE表示事件已处理禁止进一步传播,从而取消关闭操作:当返回FALSE时,事件消息进一步向上传播,此时 ...

  3. Android服务器——TomCat服务器的搭建

    Android服务器--TomCat服务器的搭建 作为一个开发人员,当然是需要自己调试一些程序的,这个时候本地的服务器就十分方便了,一般都会使用TomCat或者IIS服务器,IIS就比较简单了,其实t ...

  4. 内核调试神器SystemTap — 简介与使用(一)

    a linux trace/probe tool. 官网:https://sourceware.org/systemtap/ 简介 SystemTap是我目前所知的最强大的内核调试工具,有些家伙甚至说 ...

  5. Android开源经典项目

    目前包括: Android开源项目第一篇--个性化控件(View)篇   包括ListView.ActionBar.Menu.ViewPager.Gallery.GridView.ImageView. ...

  6. 【Qt编程】基于Qt的词典开发系列<十五>html特殊字符及正则表达式

    1.html特殊字符的显示 我们知道html语言和C语言一样也有一些特殊字符,它们是不能正常显示的,必须经过转义,在网上可以查到如何显示这些字符,如下图所示: 上图给了最常用的特殊字符的显示,下面我们 ...

  7. Android系统服务详解-android学习之旅(95)

    本文是看完android框架揭秘第六章后的总结 android系统服务提供最基本的,最稳定的核心功能,如设备控制,信息通知,通知设定,以及消息显示等,存在于Android Framework与Andr ...

  8. 重定向和servlet生命周期

    重定向(1)什么是重定向服务器通知浏览器向一个新的地址发送请求.注:可以发送一个302状态码和一个Location消息头.(该消息头包含了一个地址,称之为重定向地址),浏览器收到之后,会立即向重定向地 ...

  9. JS(作用域和闭包)

    1.对变量提升的理解 1.变量定义(上下文) 2.函数声明 2.说明 this 几种不同的使用场景 常见用法 1.作为构造函数执行 2.作为对象属性执行 3.作为普通函数执行(this === win ...

  10. Spring多数据源解决方案

    Figure 2 多数据源的选择逻辑渗透至客户端 解决方案 Figure 3 采用Proxy模式来封转数据源选择逻辑 通过采用Proxy模式我们在方案实现中实现一个虚拟的数据源.并且通过它来封装数据源 ...