python万能爬虫_新手必看|超实用Python爬虫案例集锦

世界上80%的爬虫是基于Python开发的,学好爬虫技能,可为后续的大数据分析、挖掘、机器学习等提供重要的数据源。无私分享全套Python爬虫资料,私信“学习”免费领取哦~~

作为产品运营人员,在工作中处理数据,分析数据和运用数据,基本是常态。虽非数据分析岗位,但是也是一个要重度应用数据的岗位,如果自身没有获取数据的能力,其实是非常尴尬的。

什么是爬虫?

按照一定的规则,指请求网站并获取数据的自动化程序。如果我们把互联网比作一张大的蜘蛛网,数据便是存放于蜘蛛网的各个节点,而爬虫就是一只小蜘蛛(程序),沿着网络抓取自己的猎物(数据)。

其实通俗的讲就是通过程序去获取web页面上自己想要的数据,也就是自动抓取数据。最常用领域是搜索引擎,它的基本流程是明确需求-发送请求-获取数据-解析数据-存储数据。

我们能够利用爬虫抓取图片,视频等对于你有价值的信息,只要你能通过浏览器访问的数据都可以通过爬虫获取。

Python爬虫代理

我们在做爬虫的过程中,经常会遇到这样的情况:最初爬虫正常运行,正常抓取数据,一切看起来都是那么的美好,然而一杯茶的功夫可能就会出现错误,比如403 Forbidden。出现这样的原因往往是网站采取了一些反爬虫的措施,比如,服务器会检测某个IP在单位时间内的请求次数,如果超过了某个阈值,那么服务器会直接拒绝服务,返回一些错误信息。这时候,代理IP就派上用场了。提取代理ip

很多新手朋友可能对如何使用代理IP不太了解,就拿当下最流行的爬虫语言Python来说吧。

提取http代理ip,选择提取类型,数量、协议等参数,生成api连接

urllib代理设置:

from urllib.error import URLError

from urllib.request import ProxyHandler,build_opener

proxy='123.58.10.36:8080' #使用极光http代理

#proxy='username:password@123.58.10.36:8080'

proxy_handler=ProxyHandler({

'http':'http://'+proxy,

'https':'https://'+proxy

})

opener=build_opener(proxy_handler)

try:

response=opener.open('http://httpbin.org/get') #测试ip的网址

print(response.read().decode('utf-8'))

except URLError as e:

print(e.reason)

Python语言的编写与执行

输出:print()

退出python:exit()

命令行模式:输入 python 文件名.py执行Python文件。

执行时直接输出最终结果。

Python交互模式:命令行输入python或windows--所有程序--Python--python3.7。

执行时是一行一行的执行

Python实例集锦一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?

程序分析:

假设该数为 x。

1、则:x + 100 = n2, x + 100 + 168 = m2

2、计算等式:m2 - n2 = (m + n)(m - n) = 168

x + 100 = n^2

n^2 + 168 = m^2

令 m = n+k,

2nk + k^2 = 168,

k(2n + k) = 168, 必有一个是偶数,则都为偶数,

(k/2)(k/2 + n) = 42,

i(i+n) = 42, n > 0

所以 2 <= i <= 6

for i in range(1,7):

n = 42 / i - i

if int(n) == n:

x = pow(n, 2) - 100

print(int(x))

输出结果

1581

261

21

-99

直接书写条件-列表推导式

print([pow(n,2) - 100 for m in range(168) for n in range(m) if (m-n)*(m+n) == 168])

[-99, 21, 261, 1581]输入某年某月某日,判断这一天是这一年的第几天?

date = input("输入年月日(yyyy-mm-dd):")

y,m,d = (int(i) for i in date.split('-'))

sum=0

special = (1,3,5,7,8,10)

for i in range(1,int(m)):

if i == 2:

if y%400==0 or (y%100!=0 and y%4==0):

sum+=29

else:

sum+=28

elif(i in special):

sum+=31

else:

sum+=30

sum+=d

print("这一天是一年中的第%d天"%sum)

返回结果

输入年月日(yyyy-mm-dd):2020-4-20

这一天是一年中的第111天

使用 list 存储月份信息,字典存储月份信息

months = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]

year = int(input("Year:"))

month = int(input("Month:"))

day = int(input("Day:"))

assert 0 < month < 13, "The month must be realistic"

assert 0 < day <= months[month], "The day must be realistic"

if year % 400 == 0 or (year % 4 == 0 and year % 100 != 0):

months[1] += 1

sum_day = day

for idx in range(month-1):

sum_day += months[idx]

print("It's the %dth day." % sum_day)

months = {1: 31,

2: 28,

3: 31,

4: 30,

5: 31,

6: 30,

7: 31,

8: 31,

9: 30,

10: 31,

11: 30,

12: 31}

def isLeapYear(year):

if year % 400 == 0 or (year % 4 == 0 and year % 100 != 0):

return True

else:

return False

year = int(input("Year:"))

month = int(input("Month:"))

day = int(input("Day:"))

assert 0 < month < 13, "The month must be realistic"

assert 0 < day <= months[month], "The day must be realistic"

sum_day = day

for idx in range(1, month):

sum_day += months[idx]

if isLeapYear(year):

sum_day += 1

print("It's the %dth day." % sum_day)

返回结果

Year:2020

Month:4

Day:20

It's the 111th day.

Year:2020

Month:4

Day:20

It's the 111th day.定义类

类体内定义了实例变量self.length,并定义了类的构造方法、setLen、getLen方法

class Rectangle():

def __init__(self,length,width): self.width,self.length = width,length

def setLen(self,length):

print("execute setLen")

self.length=length

def getLen(self):

print("execute getLen")

return self.length

写在最后

如果你处于想学python爬虫或者正在学习python爬虫,python爬虫的教程不少了吧,但是是最新的吗?

说不定你学了可能是两年前人家就学过的内容,在这小编分享一波2020最新的python爬虫全套教程,免费分享给大家!

获取方式:私信小编 “ 学习 ”,即可免费获取!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值