首页 > 编程语言 >Python爬虫(一)热身

Python爬虫(一)热身

时间:2022-12-28 15:36:46浏览次数:43  
标签:Python 爬虫 热身 url html Windows Agent print User


基础操作一

import urllib
import chardet #字符集检测
url = "http://www.163.com/"
html = urllib.urlopen(url)
print html.headers #头部信息
print html.getcode() #状态码,如果是200则是正常状态
print html.read() #读取内容
print html.read().decode("gbk").encode("utf-8") 编码和解码
print html.geturl() #获取网页地址
info=html.info() #头部信息
print dir(info) #网页是一个,放回对象属性key,这个很重要
info.getparam('charset') #内容字体

基础操作二

条件判断语句,自动化处理抓取结果
code = html.getcode()

print type(code) #状态吗类型
if code==200:
print "网页正常"
print html.info() #网页头部信息
else:
print "网页出现问题"

基础操作三

import urllib
#urlretrieve()方法,显示进度条
"""
1,传入网址,网址的类型一定是字符串
2.转入的,本地网址保存路径+文件名
3.一个函数的调用,我们可以任意来定义这个函数的行为,但是一定要保证这个函数有3个参数。
(1).到目前为此传递的数据块数量。
(2)每个数据块的大小,单位的byte,字节
(3)远程文件的大小。(有时候放回为-1)
"""
def callback(a , b, c):
"""
:param a:到目前为此传递的数据块数量
:param b:每个数据块的大小,单位的byte,字节
:param c:远程文件的大小
:return:
"""
down_progress =100.0*a*b/c
if down_progress > 100:
down_progress =100

print "%.2f%%" %down_progress, #逗号是一条显示进度

url = "http://www.iplaypython.com/"

local = "E:\\Other\\pythonDown\\iplaypython.html"

urllib.urlretrieve(url,local,callback)

基础操作四—-字符检测

import urllib
import chardet #字符集检测

def automatic_detect(url):

content=urllib.urlopen(url).read()
result = chardet.detect(content)
encoding = result["encoding"]
return encoding

# url = "http://www.iplaypython.com/"
# url = "http://www.163.com/"
# print automatic_detect(url)

urls=["http://www.163.com/",
"http://www.iplaypython.com/",
"http://www.jd.com/"]

for url in urls:
print url, automatic_detect(url)

基础操作五—-应对爬虫机制

import urllib2
import random

#模拟浏览器的头文件,如果可以加上代理IP。
my_headers = [
"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.110 Safari/537.36",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; TencentTraveler ; .NET CLR 1.1.4322)",
"User-Agent:Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_8; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50",
"User-Agent:Mozilla/5.0 (Windows; U; Windows NT 6.1; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50",
"User-Agent:Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0",
"User-Agent:Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0)",
"User-Agent:Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)",
"User-Agent:Opera/9.80 (Macintosh; Intel Mac OS X 10.6.8; U; en) Presto/2.8.131 Version/11.11",
"User-Agent:Opera/9.80 (Windows NT 6.1; U; en) Presto/2.8.131 Version/11.11"
]

def get_context(url,headers):
"""
:param url:
:param headers:
:return:
"""
random_header = random.choice(headers)
req=urllib2.Request(url)
req.add_header("User-Agent",random_header)
req.add_header("GET",url)
req.add_header
req.add_header("Referer")

context=urllib2.urlopen(req).read()
return context

url
print get_context(url,my_headers)

如果您喜欢我写的博文,读后觉得收获很大,不妨小额赞助我一下,让我有动力继续写出高质量的博文,感谢您的赞赏!微信


Python爬虫(一)热身_html


标签:Python,爬虫,热身,url,html,Windows,Agent,print,User
From: https://blog.51cto.com/u_15649751/5975072

相关文章

  • 当我把用Python做的课堂点名系统献给各科老师后,再也没挂过科
    刚上大学的表弟问我,大学准备好好玩玩,问我有没有什么不挂科的秘诀。哎,这可就问对人了,要想不挂科,先把老师贿赂好,当然,咱们说的贿赂不是送钱啥的,这不是侮辱老师吗?于是我......
  • python中resp.json()与json.loads(str)的区别
    resp=resquests.get(url)print(type(resp))#<class'requests.models.Response'>第一行代码使用requests库发送get请求,得到响应数据resp。第二行代码的输......
  • 读python代码-学到的python
    1.withopen(data_path,'r')asf:withopen()是python用来打开本地文件的,他会在使用完毕后,自动关闭文件,无需手动书写close().三种打开模式:r:只读 用read()w:只写用w......
  • python模块之psutil详解
     一、psutil模块:1.psutil是一个跨平台库(​​http://pythonhosted.org/psutil/​​)能够轻松实现获取系统运行的进程和系统利用率(包括CPU、内存、磁盘、网络等)信息。它主......
  • 【python】抽象基类 from abc import ABC, abstractmethod
    abc模块作用Python本身不提供抽象类和接口机制,要想实现抽象类,可以借助abc模块。ABC是Abstract BaseClass的缩写。假设我们定义一些抽象方法,然后子类继承的时候必须要重......
  • 爬虫逆向 - 分析思路
    背景:网站分析思路 笔记1:案例:1.乌海市公共资源:http://www.whggzy.com/home.html数据加密案例2.福建省公共资源交易电子公共服务平台:https://ggzyfw.fu......
  • 逆向工程 Python 逆向
    逆向工程Python逆向Salarypython逆向https://github.com/SKPrimin/HomeWork/ReverseEngineering/lab1_python(选做)运行Salary.pyc,要求输出flag代表成功。直接运行......
  • python的list的用法
    #ReadMe#本工具是根据用户选择的条目来打印该列表下的内容#例如选择“北京”就会打印北京下面的“海淀”“昌平”“朝阳”,选择“海淀”然后会打印海淀下面的“清华大学”和......
  • Python encode()方法和decode()方法
    Pythonencode()方法encode()方法为字符串类型(str)提供的方法,用于将str类型转换成bytes类型,这个过程也称为“编码”。encode()方法的语法格式如下:str.encode([enco......
  • python以主程序形式运行
    以主程序形式运行在每个模块的定义中都包括一个记录模块名称的变量__name__,程序可以检查该变量,以确定他们在哪个模块中执行。如果一个模块不是被导入到其它程序中执行,那么它......