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

Python爬虫代理

urllib代理设置：

from urllib.error import URLError

from urllib.request import ProxyHandler,build_opener

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

proxy_handler=ProxyHandler({

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

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

})

opener=build_opener(proxy_handler)

try:

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

except URLError as e:

print(e.reason)

Python语言的编写与执行

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

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

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

2nk + k^2 = 168,

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

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

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

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)

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.定义类

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