requests 使用默认 cookies

python爬虫李魔佛 发表了文章 • 0 个评论 • 5993 次浏览 • 2017-03-17 01:41 • 来自相关话题

对于一些网站,为了防止爬虫,必须要有cookies信息,header中的cookies字段不能空。
使用requests库的时候,可以初始化一次cookies信息,后面的回话就能够一直用这个cookies了。session=requests.session()
#先创建一个seession

s=session.get('http://xueqiu.com',headers=self.headers)
#随意建立一个访问雪球的session,此时的session就自带了雪球的cookies

url='https://xueqiu.com/snowmart/push/stocks.json?product_id=19&page=3&count=5'
headers['Referer']='https://xueqiu.com/strategy/19'
headers['X-Requested-With']='XMLHttpRequest'
headers['DNT']='1'

data={'product_id':19,'page':3,'count':5}

resp=session.get(url,headers=self.headers,params=data).text
#如果这里用的request.get的话,就不能获取到网页返回的正确内容
print resp 这里需要注意的是,第一次

s=session.get('http://xueqiu.com',headers=self.headers) #随意建立一个访问雪球的session,此时的session就自带了雪球的cookies
 
是必须的,如果没有只一次正常访问,cookies就不能保存下来供下一次正常使用。
 
 
2017-3-28 更新
一般来说,除非是为了每天定期跑的或者破解他人密码这些,可以不用管他们的登录问题,可以很直接的在浏览器中,把你自己账号的cookie信息写进到request的header里面,完全可以访问。
 
所以一旦cookie被别人获取了,他们就可以构造数据,去获取你账号的相关信息。 (cookie应该和你的电脑硬件没关系的吧? 因为我曾经在几台机器上用同一个cookie获取我登录了的账号,也是没有问题的)
 
举个栗子:

def csdn():
session=requests.session()
header={'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.82 Safari/537.36'}
url='[url]http://msg.csdn.net/'[/url] header['Cookie']='uuid_tt_dd=-5697318013068753627_20160111; _ga=GA1.2.795042232.1452766190; _message_m=quvq2wle24wa4awe UserInfo=zMhiNgesgIlEBQ3TOqLCtx4nUI360IIq3ciBzg4EKH%2FW8mSpTANpu5cTlRFLj2Tqh%2FZzQr2rNqDtT1SZz%2Be2%2FpDkGoQxDK3IVUZXvwZ%2FEP1I4UTg6MoZkH7LDO3sjrJJ; UserNick=%E9%87%8D%E5%A4%8D%E7%9A%84%E7%94%9F%E6%B4%BB; AU=0F1; UD=%E8%AE%B0%E5%BD%95%E8%87%AA%E5%AD%A6%E7%9A%84%E5%8E%86%E7%A8%8B+%E5%88%83%E8%8D%; BT=1490707396344; access-token=c2e12bff-5b27-4a91-953b-448ff6f6beac; _csdn_notify_admin_session=VE41a0d3TitrVGY2bGtXY09pZENwR1lHenhUU1NVaWc1b04wL1I3dCtDQVdadWpjMXBzdGRJL0RZR04wYldvZDBhTU96b2oycVVKeVI1UEVyUHFKbG1yNnB2b2pHRWVnWG1uc2JMM2R3YWthakRyTXZNaEpVU1NtUy9zQUJrNjd3R2lpbG5PK0paMnlyc1dyK0lTZUtRPT0tLXlPQUE1QzF5UmhDNjEvSFdtRFlQS2c9PQ%3D%3D--4569c5a32916dcf969a8b7e007c37abeb90be4f3; dc_tos=onj1dg; dc_session_id=1490707393375; Hm_lvt_6bcd52f51e9b3dce32bec4a3997715ac=1490024083,1490024559,1490374375,1490707368; Hm_lpvt_6bcd52f51e9b3dce32bec4a3997715ac=1490707638' header['Cache-Control']='max-age=0'
header['Host']='msg.csdn.net'
header['Referer']='http://blog.csdn.net/username'
resp=requests.get(url,headers=header)
print resp.text
 
上面这段代码,只要你改一下cookie和 Refer 的连接,改成你自己的用户名。 你就可以访问你的csdn的消息,也就是别人在你的csdn博客访问的的留言,评论。
 
只是这个cookie只能在某个时间段生效,也就是存活期大概就一个星期左右。

 抓包技术过硬,何须模拟登陆?
 
 
http://30daydo.com/publish/article/155
  查看全部
对于一些网站,为了防止爬虫,必须要有cookies信息,header中的cookies字段不能空。
使用requests库的时候,可以初始化一次cookies信息,后面的回话就能够一直用这个cookies了。
session=requests.session()
#先创建一个seession

s=session.get('http://xueqiu.com',headers=self.headers)
#随意建立一个访问雪球的session,此时的session就自带了雪球的cookies

url='https://xueqiu.com/snowmart/push/stocks.json?product_id=19&page=3&count=5'
headers['Referer']='https://xueqiu.com/strategy/19'
headers['X-Requested-With']='XMLHttpRequest'
headers['DNT']='1'

data={'product_id':19,'page':3,'count':5}

resp=session.get(url,headers=self.headers,params=data).text
#如果这里用的request.get的话,就不能获取到网页返回的正确内容
print resp
 这里需要注意的是,第一次

s=session.get('http://xueqiu.com',headers=self.headers) #随意建立一个访问雪球的session,此时的session就自带了雪球的cookies
 
是必须的,如果没有只一次正常访问,cookies就不能保存下来供下一次正常使用。
 
 
2017-3-28 更新
一般来说,除非是为了每天定期跑的或者破解他人密码这些,可以不用管他们的登录问题,可以很直接的在浏览器中,把你自己账号的cookie信息写进到request的header里面,完全可以访问。
 
所以一旦cookie被别人获取了,他们就可以构造数据,去获取你账号的相关信息。 (cookie应该和你的电脑硬件没关系的吧? 因为我曾经在几台机器上用同一个cookie获取我登录了的账号,也是没有问题的)
 
举个栗子:


def csdn():
session=requests.session()
header={'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.82 Safari/537.36'}
url='[url]http://msg.csdn.net/'[/url]
    header['Cookie']='uuid_tt_dd=-5697318013068753627_20160111; _ga=GA1.2.795042232.1452766190; _message_m=quvq2wle24wa4awe UserInfo=zMhiNgesgIlEBQ3TOqLCtx4nUI360IIq3ciBzg4EKH%2FW8mSpTANpu5cTlRFLj2Tqh%2FZzQr2rNqDtT1SZz%2Be2%2FpDkGoQxDK3IVUZXvwZ%2FEP1I4UTg6MoZkH7LDO3sjrJJ; UserNick=%E9%87%8D%E5%A4%8D%E7%9A%84%E7%94%9F%E6%B4%BB; AU=0F1; UD=%E8%AE%B0%E5%BD%95%E8%87%AA%E5%AD%A6%E7%9A%84%E5%8E%86%E7%A8%8B+%E5%88%83%E8%8D%; BT=1490707396344; access-token=c2e12bff-5b27-4a91-953b-448ff6f6beac; _csdn_notify_admin_session=VE41a0d3TitrVGY2bGtXY09pZENwR1lHenhUU1NVaWc1b04wL1I3dCtDQVdadWpjMXBzdGRJL0RZR04wYldvZDBhTU96b2oycVVKeVI1UEVyUHFKbG1yNnB2b2pHRWVnWG1uc2JMM2R3YWthakRyTXZNaEpVU1NtUy9zQUJrNjd3R2lpbG5PK0paMnlyc1dyK0lTZUtRPT0tLXlPQUE1QzF5UmhDNjEvSFdtRFlQS2c9PQ%3D%3D--4569c5a32916dcf969a8b7e007c37abeb90be4f3; dc_tos=onj1dg; dc_session_id=1490707393375; Hm_lvt_6bcd52f51e9b3dce32bec4a3997715ac=1490024083,1490024559,1490374375,1490707368; Hm_lpvt_6bcd52f51e9b3dce32bec4a3997715ac=1490707638'
    header['Cache-Control']='max-age=0'
header['Host']='msg.csdn.net'
header['Referer']='http://blog.csdn.net/username'
resp=requests.get(url,headers=header)
print resp.text

 
上面这段代码,只要你改一下cookie和 Refer 的连接,改成你自己的用户名。 你就可以访问你的csdn的消息,也就是别人在你的csdn博客访问的的留言,评论。
 
只是这个cookie只能在某个时间段生效,也就是存活期大概就一个星期左右。


 抓包技术过硬,何须模拟登陆?
 
 
http://30daydo.com/publish/article/155
 

sys.std.write和print的区别

李魔佛 发表了文章 • 0 个评论 • 2649 次浏览 • 2017-03-13 22:00 • 来自相关话题

看别人写的程序,很多地方用sys.std.write,甚少用print。
于是google了下,主要区别在于sys.std.write输出没有换行符,而使用print有换行符
 
引用stackoverflow的例子

print 99


等同于
import sys 
sys.stdout.write(str(99) + '\n')

  查看全部
看别人写的程序,很多地方用sys.std.write,甚少用print。
于是google了下,主要区别在于sys.std.write输出没有换行符,而使用print有换行符
 
引用stackoverflow的例子

print 99


等同于
import sys 
sys.stdout.write(str(99) + '\n')

 

淘宝试用 每天自动申请 python程序

李魔佛 发表了文章 • 2 个评论 • 3658 次浏览 • 2017-03-13 16:39 • 来自相关话题

手机上安装淘宝,并且登陆你的帐号,然后连接usb线到电脑,运行下面的程序
 
def taobao_shiyong():

d.screen.on()
d.press.home()

activity_name='com.taobao.taobao/com.taobao.tao.homepage.MainActivity3'
launch_app(activity_name)
mid_x=displayWidth/2

try:

d(text=u'我的淘宝').click()
time.sleep(3)
d(text='查看更多工具').click()
time.sleep(3)
d(scrollable=True).scroll.to(text=u'免费试用')
time.sleep(2)
d(text=u'免费试用').click()
time.sleep(3)


delta_y=144
full_y=1920
full_x=1080
fix_x=880
origin_y=222
d.swipe(fix_x,full_y-delta_y,fix_x,origin_y)
time.sleep(3)
#d.swipe(fix_x,952,fix_x,origin_y)
time.sleep(5)

sumakeji=displayWidth/8*3
jiayongdianqi=displayWidth/8*5
d.click(jiayongdianqi,1660)
time.sleep(3)

#each_dianpu()
#d.click(jiayongdianqi,300)

delta_each=400
time.sleep(3)

for dragtime in range(20):
for i in range(3):
d.click(919,600+i*delta_each)
time.sleep(8)
each_dianpu()

d.swipe(919,1600,919,400)

except:
print "Can't find items"
程序会一直运行,等到达到每天申请的最大次数。
  查看全部
手机上安装淘宝,并且登陆你的帐号,然后连接usb线到电脑,运行下面的程序
 
def taobao_shiyong():

d.screen.on()
d.press.home()

activity_name='com.taobao.taobao/com.taobao.tao.homepage.MainActivity3'
launch_app(activity_name)
mid_x=displayWidth/2

try:

d(text=u'我的淘宝').click()
time.sleep(3)
d(text='查看更多工具').click()
time.sleep(3)
d(scrollable=True).scroll.to(text=u'免费试用')
time.sleep(2)
d(text=u'免费试用').click()
time.sleep(3)


delta_y=144
full_y=1920
full_x=1080
fix_x=880
origin_y=222
d.swipe(fix_x,full_y-delta_y,fix_x,origin_y)
time.sleep(3)
#d.swipe(fix_x,952,fix_x,origin_y)
time.sleep(5)

sumakeji=displayWidth/8*3
jiayongdianqi=displayWidth/8*5
d.click(jiayongdianqi,1660)
time.sleep(3)

#each_dianpu()
#d.click(jiayongdianqi,300)

delta_each=400
time.sleep(3)

for dragtime in range(20):
for i in range(3):
d.click(919,600+i*delta_each)
time.sleep(8)
each_dianpu()

d.swipe(919,1600,919,400)

except:
print "Can't find items"

程序会一直运行,等到达到每天申请的最大次数。
 

python 爬虫 urllib/requests 中文乱码 终极解决

python爬虫李魔佛 发表了文章 • 0 个评论 • 6728 次浏览 • 2017-03-11 16:17 • 来自相关话题

如果使用的是reqests的包 content=requests.get(url,headers=self.header)
content.encoding='gbk'
print content.text 一般情况 是直接可以content.text 就可以输出网页的内容,不过如果网页的编码不是utf-8的话,需要手动编码
content.encoding='gbk' 
这样content.text就可以正常显示中文。
 
  查看全部
如果使用的是reqests的包
            content=requests.get(url,headers=self.header)
content.encoding='gbk'
print content.text
 一般情况 是直接可以content.text 就可以输出网页的内容,不过如果网页的编码不是utf-8的话,需要手动编码
content.encoding='gbk' 
这样content.text就可以正常显示中文。
 
 

Day6 leetcode Fizz Buzz 字符转换游戏(实在不知道该怎么翻译这个)

李魔佛 发表了文章 • 0 个评论 • 4074 次浏览 • 2017-03-07 11:06 • 来自相关话题

Fizz BuzzWrite a program that outputs the string representation of numbers from 1 to n.

But for multiples of three it should output “Fizz” instead of the number and for the multiples of five output “Buzz”. For numbers which are multiples of both three and five output “FizzBuzz”.

Example:
n = 15,

Return:
[
"1",
"2",
"Fizz",
"4",
"Buzz",
"Fizz",
"7",
"8",
"Fizz",
"Buzz",
"11",
"Fizz",
"13",
"14",
"FizzBuzz"
]
中文解释:
输入一个数字,返回从1到这个数字的所有数字的字符串,如果遇到3的倍数,就替换成Fizz, 遇到5的倍数,就tihu替换成Buzz,同时是3和5的倍数,就tihu替换成FizzBuzz。
 
我的代码:
def fizzBuzz(self, n):
"""
:type n: int
:rtype: List[str]
"""

result=[]
for i in range(1,n+1):
remainder1=i%3
remainder2=i%5
if remainder1 != 0 and remainder2!=0:
result.append(str(i))
elif remainder1==0 and remainder2!=0:
result.append('Fizz')
elif remainder1!=0 and remainder2 == 0:
result.append("Buzz")
else:
result.append("FizzBuzz")

return result 查看全部
Fizz BuzzWrite a program that outputs the string representation of numbers from 1 to n.

But for multiples of three it should output “Fizz” instead of the number and for the multiples of five output “Buzz”. For numbers which are multiples of both three and five output “FizzBuzz”.

Example:
n = 15,

Return:
[
"1",
"2",
"Fizz",
"4",
"Buzz",
"Fizz",
"7",
"8",
"Fizz",
"Buzz",
"11",
"Fizz",
"13",
"14",
"FizzBuzz"
]

中文解释:
输入一个数字,返回从1到这个数字的所有数字的字符串,如果遇到3的倍数,就替换成Fizz, 遇到5的倍数,就tihu替换成Buzz,同时是3和5的倍数,就tihu替换成FizzBuzz。
 
我的代码:
    def fizzBuzz(self, n):
"""
:type n: int
:rtype: List[str]
"""

result=[]
for i in range(1,n+1):
remainder1=i%3
remainder2=i%5
if remainder1 != 0 and remainder2!=0:
result.append(str(i))
elif remainder1==0 and remainder2!=0:
result.append('Fizz')
elif remainder1!=0 and remainder2 == 0:
result.append("Buzz")
else:
result.append("FizzBuzz")

return result

Day5 leetcode Next Greater Element 下一个更大的元素

李魔佛 发表了文章 • 0 个评论 • 4112 次浏览 • 2017-03-03 09:30 • 来自相关话题

You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of nums2. Find all the next greater numbers for nums1's elements in the corresponding places of nums2.

The Next Greater Number of a number x in nums1 is the first greater number to its right in nums2. If it does not exist, output -1 for this number.
 
Example 1:
Input: nums1 = [4,1,2], nums2 = [1,3,4,2].
Output: [-1,3,-1]
Explanation:
For number 4 in the first array, you cannot find the next greater number for it in the second array, so output -1.
For number 1 in the first array, the next greater number for it in the second array is 3.
For number 2 in the first array, there is no next greater number for it in the second array, so output -1.Example 2:
Input: nums1 = [2,4], nums2 = [1,2,3,4].
Output: [3,-1]
Explanation:
For number 2 in the first array, the next greater number for it in the second array is 3.
For number 4 in the first array, there is no next greater number for it in the second array, so
Note:


All elements in nums1 and nums2 are unique.
The length of both nums1 and nums2 would not exceed 1000.
 
中文解释下:
有2个数组(列表) num1,和num2,

nums1 = [4,1,2], nums2 = [1,3,4,2].
 
nums1是num2的子集
 
然后在nums1中每个元素,在num2中找到第一个比它大的元素,比如nums1中第一个是4,在nums2中没有比4更大的,所以返回的是-1,nums第二个是1,在nums2中第一个比1大的是3,所以返回的是3,第三个的是2,nums2中第一个比2大的数是3,所以返回的是3
所以上面的结果需要返回:
[-1,3,3] 查看全部
You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of nums2. Find all the next greater numbers for nums1's elements in the corresponding places of nums2.

The Next Greater Number of a number x in nums1 is the first greater number to its right in nums2. If it does not exist, output -1 for this number.
 
Example 1:
Input: nums1 = [4,1,2], nums2 = [1,3,4,2].
Output: [-1,3,-1]
Explanation:
For number 4 in the first array, you cannot find the next greater number for it in the second array, so output -1.
For number 1 in the first array, the next greater number for it in the second array is 3.
For number 2 in the first array, there is no next greater number for it in the second array, so output -1.
Example 2:
Input: nums1 = [2,4], nums2 = [1,2,3,4].
Output: [3,-1]
Explanation:
For number 2 in the first array, the next greater number for it in the second array is 3.
For number 4 in the first array, there is no next greater number for it in the second array, so

Note:


All elements in nums1 and nums2 are unique.
The length of both nums1 and nums2 would not exceed 1000.

 
中文解释下:
有2个数组(列表) num1,和num2,

nums1 = [4,1,2], nums2 = [1,3,4,2].
 
nums1是num2的子集
 
然后在nums1中每个元素,在num2中找到第一个比它大的元素,比如nums1中第一个是4,在nums2中没有比4更大的,所以返回的是-1,nums第二个是1,在nums2中第一个比1大的是3,所以返回的是3,第三个的是2,nums2中第一个比2大的数是3,所以返回的是3
所以上面的结果需要返回:
[-1,3,3]

leetcode Day4 Keyboard Row 键盘中的行

李魔佛 发表了文章 • 0 个评论 • 4699 次浏览 • 2017-02-28 22:21 • 来自相关话题

Given a List of words, return the words that can be typed using letters of alphabet on only one row's of American keyboard like the image below.





 
Example 1:
Input: ["Hello", "Alaska", "Dad", "Peace"]
Output: ["Alaska", "Dad"]Note:


You may use one character in the keyboard more than once.
You may assume the input string will only contain letters of alphabet.
 
中文解释:
输入一个字符串列表,如果这个字符串的字母在键盘上的位置为同一行,就输出这个字符串,否则不输出。
 
def findWords( words):
"""
:type words: List[str]
:rtype: List[str]
"""

kb={'q':['q','w','e','r','t','y','u','i','o','p'],
'a':['a','s','d','f','g','h','j','k','l'],
'z':['z','x','c','v','b','n','m']}

rList=[]
qRow= kb['q']
aRow= kb['a']
zRow= kb['z']
for wi in words:
w=wi.lower()
i=0
l=len(w)

if w[0] in qRow:
row=qRow
if w[0] in aRow:
row=aRow
if w[0] in zRow:
row=zRow

#row=kb[w[0]]
for i in range(len(w)):
if w[i] not in row:
break
else:
if i==l-1:
rList.append(wi)
return rList
解释:
首先将字母转换为统一的小写字母,然后根据首字母确定该字符串会属于键盘上的哪一列,因为键盘上只有3列,分别为q,a,z行,如果确定属于q行,接下来将剩下的字符一直在q行内循环,一旦遇到不在q行,就退出这一次的循环,进行下一个字符串的判断。
  查看全部
Given a List of words, return the words that can be typed using letters of alphabet on only one row's of American keyboard like the image below.

keyboard.png

 
Example 1:
Input: ["Hello", "Alaska", "Dad", "Peace"]
Output: ["Alaska", "Dad"]
Note:


You may use one character in the keyboard more than once.
You may assume the input string will only contain letters of alphabet.
 
中文解释:
输入一个字符串列表,如果这个字符串的字母在键盘上的位置为同一行,就输出这个字符串,否则不输出。
 
    def findWords( words):
"""
:type words: List[str]
:rtype: List[str]
"""

kb={'q':['q','w','e','r','t','y','u','i','o','p'],
'a':['a','s','d','f','g','h','j','k','l'],
'z':['z','x','c','v','b','n','m']}

rList=[]
qRow= kb['q']
aRow= kb['a']
zRow= kb['z']
for wi in words:
w=wi.lower()
i=0
l=len(w)

if w[0] in qRow:
row=qRow
if w[0] in aRow:
row=aRow
if w[0] in zRow:
row=zRow

#row=kb[w[0]]
for i in range(len(w)):
if w[i] not in row:
break
else:
if i==l-1:
rList.append(wi)
return rList

解释:
首先将字母转换为统一的小写字母,然后根据首字母确定该字符串会属于键盘上的哪一列,因为键盘上只有3列,分别为q,a,z行,如果确定属于q行,接下来将剩下的字符一直在q行内循环,一旦遇到不在q行,就退出这一次的循环,进行下一个字符串的判断。
 

leetcode Day3 complement number 【补码】

李魔佛 发表了文章 • 0 个评论 • 3303 次浏览 • 2017-02-27 21:47 • 来自相关话题

Given a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representation.

Note:


The given integer is guaranteed to fit within the range of a 32-bit signed integer.
You could assume no leading zero bit in the integer’s binary representation.

Example 1:
 
Input: 5
Output: 2
Explanation: The binary representation of 5 is 101 (no leading zero bits), and its complement is 010. So you need to output 2.
Input: 1
Output: 0
Explanation: The binary representation of 1 is 1 (no leading zero bits), and its complement is 0. So you need to output 0.
解决方法:
class Solution(object):
def findComplement(self, num):
"""
:type num: int
:rtype: int
"""
result=[]
while num!=0:
remainder=num%2
num=num/2
result.append(remainder)
l=len(result)

for i in range(l/2):
temp=result[i]
result[i]=result[l-i-1]
result[l-i-1]=temp
lam=lambda x:abs(x-1)

for i in range(l):
result[i]=lam(result[i])

#print result
sum=0
for i in range(l):
sum=sum+result[l-i-1]*pow(2,i)
return sum
PS: 上面的方法是第一次开始做的最原始的方法,巨傻无比。 完全就是一个没学过计算机原理或者微机原理的人的代码哈。 连一个数的补码这种计算机第一节课的内容都还给老师了。。
 
附上最简便的一个解法:
class Solution(object):
def findComplement(self, num):
i = 1
while i <= num:
i = i << 1
return (i - 1) ^ num
以为其实一个补码就是一个数与另外一个全1的数进行异或处理哈。
鄙视自己!!!
 
  查看全部
Given a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representation.

Note:


The given integer is guaranteed to fit within the range of a 32-bit signed integer.
You could assume no leading zero bit in the integer’s binary representation.

Example 1:
 
Input: 5
Output: 2
Explanation: The binary representation of 5 is 101 (no leading zero bits), and its complement is 010. So you need to output 2.

Input: 1
Output: 0
Explanation: The binary representation of 1 is 1 (no leading zero bits), and its complement is 0. So you need to output 0.

解决方法:
class Solution(object):
def findComplement(self, num):
"""
:type num: int
:rtype: int
"""
result=[]
while num!=0:
remainder=num%2
num=num/2
result.append(remainder)
l=len(result)

for i in range(l/2):
temp=result[i]
result[i]=result[l-i-1]
result[l-i-1]=temp
lam=lambda x:abs(x-1)

for i in range(l):
result[i]=lam(result[i])

#print result
sum=0
for i in range(l):
sum=sum+result[l-i-1]*pow(2,i)
return sum

PS: 上面的方法是第一次开始做的最原始的方法,巨傻无比。 完全就是一个没学过计算机原理或者微机原理的人的代码哈。 连一个数的补码这种计算机第一节课的内容都还给老师了。。
 
附上最简便的一个解法:
class Solution(object):
def findComplement(self, num):
i = 1
while i <= num:
i = i << 1
return (i - 1) ^ num

以为其实一个补码就是一个数与另外一个全1的数进行异或处理哈。
鄙视自己!!!
 
 

leetcode Day2 Hamming Distance 海明距离

李魔佛 发表了文章 • 0 个评论 • 3756 次浏览 • 2017-02-26 21:57 • 来自相关话题

The Hamming distance between two integers is the number of positions at which the corresponding bits are different.

Given two integers x and y, calculate the Hamming distance.

Note:
0 ≤ x, y < 231.

Example:
 
Input: x = 1, y = 4

Output: 2

Explanation:
1 (0 0 0 1)
4 (0 1 0 0)
↑ ↑

The above arrows point to positions where the corresponding bits are different.

中文的大概意思是,两个数取异或,然后看异或后,1的个数,也就是二进制数中不同位的个数。 这编码常用于信道编码。
 
python中异或用符号^, 然后python自带一个函数bin(),可以把一个数转换为二进制。
转为为二进制后,再用一个循环计算1出现的个数。
 
上代码:
 
x=10
y=20
z=x^y
#solution 1
s=bin(z)
print type(s)
distance=0
for i in s[2:]:
if i=='1':
distance+=1
print "distance is %d" %distance 查看全部
The Hamming distance between two integers is the number of positions at which the corresponding bits are different.

Given two integers x and y, calculate the Hamming distance.

Note:
0 ≤ x, y < 231.

Example:
 
Input: x = 1, y = 4

Output: 2

Explanation:
1 (0 0 0 1)
4 (0 1 0 0)
↑ ↑

The above arrows point to positions where the corresponding bits are different.

中文的大概意思是,两个数取异或,然后看异或后,1的个数,也就是二进制数中不同位的个数。 这编码常用于信道编码。
 
python中异或用符号^, 然后python自带一个函数bin(),可以把一个数转换为二进制。
转为为二进制后,再用一个循环计算1出现的个数。
 
上代码:
 
x=10
y=20
z=x^y
#solution 1
s=bin(z)
print type(s)
distance=0
for i in s[2:]:
if i=='1':
distance+=1
print "distance is %d" %distance

leetcode Day1 Two Sum 两数之和

李魔佛 发表了文章 • 0 个评论 • 3737 次浏览 • 2017-02-25 18:39 • 来自相关话题

题目:
Given an array of integers, return indices of the two numbers such that they add up to a specific target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

Example:
Given 
nums = [2, 7, 11, 15], target = 9, 
Because nums[0] + nums[1] = 2 + 7 = 9, 
return [0, 1].
 
中文意思就是,给定输入一个列表和一个目标数, 输出的是两个数的下标,这个数的对应的值相加,等于目标数。
 
上代码:

def twoSum(nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
indics=[]
for i in range(len(nums)-1):
#使用两次循环,类似于冒泡算法,尝试每个数和另外一个数的和,枚举
for j in range(i+1,len(nums)):
if target== nums[i]+nums[j]:
indics.append(i)
indics.append(j)
return indics
  查看全部
题目:
Given an array of integers, return indices of the two numbers such that they add up to a specific target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

Example:
Given 
nums = [2, 7, 11, 15], target = 9, 
Because nums[0] + nums[1] = 2 + 7 = 9, 
return [0, 1].
 
中文意思就是,给定输入一个列表和一个目标数, 输出的是两个数的下标,这个数的对应的值相加,等于目标数。
 
上代码:

def twoSum(nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
indics=[]
for i in range(len(nums)-1):
#使用两次循环,类似于冒泡算法,尝试每个数和另外一个数的和,枚举
for j in range(i+1,len(nums)):
if target== nums[i]+nums[j]:
indics.append(i)
indics.append(j)
return indics

 

python 遍历文件夹 删除 txt后缀的文件(或者其他符合规律的文件)

李魔佛 发表了文章 • 0 个评论 • 4655 次浏览 • 2017-02-19 21:23 • 来自相关话题

使用cabilre生产的电子书文件目录下,会有一个txt和aw3格式的两个文件,但是放入电子书kindle中只需要一个aw3就好了,不然重复的文件会在kindle上显示2本一样的书
 
代码如下:
# -*-coding=utf-8-*-
__author__ = 'Rocky'
import os,re

cwd=os.getcwd()
p=re.compile('\.txt')
print cwd
for dirpath, dirname,filename in os.walk(cwd):
#print dirpath,dirname,filename
#print dirpath
print dirname
print type(filename)
if filename is not None:
for i in filename:
#if filename is not None:

if p.search(i):
os.remove(os.path.join(dirpath,i)) 查看全部
使用cabilre生产的电子书文件目录下,会有一个txt和aw3格式的两个文件,但是放入电子书kindle中只需要一个aw3就好了,不然重复的文件会在kindle上显示2本一样的书
 
代码如下:
# -*-coding=utf-8-*-
__author__ = 'Rocky'
import os,re

cwd=os.getcwd()
p=re.compile('\.txt')
print cwd
for dirpath, dirname,filename in os.walk(cwd):
#print dirpath,dirname,filename
#print dirpath
print dirname
print type(filename)
if filename is not None:
for i in filename:
#if filename is not None:

if p.search(i):
os.remove(os.path.join(dirpath,i))

在学习装饰器的过程中遇到的奇怪的输出

回复

李魔佛 发起了问题 • 1 人关注 • 0 个回复 • 4636 次浏览 • 2017-02-09 18:56 • 来自相关话题

淘宝每天自动领取金币/京东每天自动领取金豆/苏宁打卡获取云钻

李魔佛 发表了文章 • 8 个评论 • 16271 次浏览 • 2017-02-09 18:31 • 来自相关话题

看到某些人天天手动打卡,领取这些小奖励。虽然很少,不过,也是财富嘛。 白送白不要。 这些奖励在购物的时候可以抵消相应的金额。
 
所以写了个小程序来模拟人工,每天帮你自动打卡哈。(需要连接你的手机在电脑,然后执行程序。)
手机配置是1920x1080的屏幕分辨率,如果不是的话修改下代码(主要是触摸的坐标),就可以运行的啦。
 #京东京豆
def jd():
d.screen.on()
d.press.home()
activity_name='com.jingdong.app.mall/.main.MainActivity'
launch_app(activity_name)

if not d(text=u'领京豆').wait.exists(timeout=20*1000):
print "Failed to get the page"
return
d(text=u'领京豆').click()
dou_x=853
dou_y=400

#glaxy_x=yun_x*gallery*full/cuizi_full
time.sleep(15)
d.click(dou_x,dou_y)
print "JD done" 
#苏宁云钻
def suning():
#苏宁在6点之后
global displayWidth
global displayHeight
d.screen.on()
d.press.home()
'''
#解锁,没有密码的情况下
sx=560
sy=1700
ex=560
ey=900
#d.swipe(sx,sy,ex,ey,steps=2)

#d(scrollable=True).fling.horiz.forward()
#d(text=u'苏宁易购').swipe.right()
home_swipe_sx=950
home_swipe_sy=1350
home_swipe_ex=450
home_swipe_ey=1350
while not d(text=u"苏宁易购").exists:
d.swipe(home_swipe_sx,home_swipe_sy,home_swipe_ex,home_swipe_ey,steps=2)
time.sleep(3)
d(text=u'苏宁易购').click()
#time.sleep(10)
'''
activity_name='com.suning.mobile.ebuy/.base.host.InitialActivity'
launch_app(activity_name)
if not d(text=u'领云钻').wait.exists(timeout=20*1000):
print "Failed to get the page"
return
d(text=u'领云钻').click()
yun_x=372
yun_y=1524

#glaxy_x=yun_x*gallery*full/cuizi_full
time.sleep(15)
d.click(yun_x,yun_y)
time.sleep(10)

daka_x=displayWidth/2
daka_y=displayHeight/2
d.click(daka_x,daka_y)
time.sleep(20)
print "Sunning Done"
#淘宝的金币
def taobao_cuizi():
d.screen.on()
d.press.home()
#解锁,没有密码的情况下
'''
sx=560
sy=1700
ex=560
ey=900
#d.swipe(sx,sy,ex,ey,steps=2)

#d(scrollable=True).fling.horiz.forward()
home_swipe_sx=950
home_swipe_sy=1350
home_swipe_ex=450
home_swipe_ey=1350
while not d(text=u"手机淘宝").exists:
d.swipe(home_swipe_sx,home_swipe_sy,home_swipe_ex,home_swipe_ey,steps=2)
time.sleep(3)
d(text=u'手机淘宝').click()
'''
activity_name='com.taobao.taobao/com.taobao.tao.homepage.MainActivity3'
launch_app(activity_name)
if d(text=u'领金币').wait.exists(timeout=12*1000):
#print "Dismiss update"
d(text=u'领金币').click()
#登录账号,刷新下即可

time.sleep(15)
jb_x=900
jb_y=370
d.click(jb_x,jb_y)

完整代码:
https://github.com/Rockyzsu/red_bag
  查看全部
看到某些人天天手动打卡,领取这些小奖励。虽然很少,不过,也是财富嘛。 白送白不要。 这些奖励在购物的时候可以抵消相应的金额。
 
所以写了个小程序来模拟人工,每天帮你自动打卡哈。(需要连接你的手机在电脑,然后执行程序。)
手机配置是1920x1080的屏幕分辨率,如果不是的话修改下代码(主要是触摸的坐标),就可以运行的啦。
 
#京东京豆
def jd():
d.screen.on()
d.press.home()
activity_name='com.jingdong.app.mall/.main.MainActivity'
launch_app(activity_name)

if not d(text=u'领京豆').wait.exists(timeout=20*1000):
print "Failed to get the page"
return
d(text=u'领京豆').click()
dou_x=853
dou_y=400

#glaxy_x=yun_x*gallery*full/cuizi_full
time.sleep(15)
d.click(dou_x,dou_y)
print "JD done"
 
#苏宁云钻
def suning():
#苏宁在6点之后
global displayWidth
global displayHeight
d.screen.on()
d.press.home()
'''
#解锁,没有密码的情况下
sx=560
sy=1700
ex=560
ey=900
#d.swipe(sx,sy,ex,ey,steps=2)

#d(scrollable=True).fling.horiz.forward()
#d(text=u'苏宁易购').swipe.right()
home_swipe_sx=950
home_swipe_sy=1350
home_swipe_ex=450
home_swipe_ey=1350
while not d(text=u"苏宁易购").exists:
d.swipe(home_swipe_sx,home_swipe_sy,home_swipe_ex,home_swipe_ey,steps=2)
time.sleep(3)
d(text=u'苏宁易购').click()
#time.sleep(10)
'''
activity_name='com.suning.mobile.ebuy/.base.host.InitialActivity'
launch_app(activity_name)
if not d(text=u'领云钻').wait.exists(timeout=20*1000):
print "Failed to get the page"
return
d(text=u'领云钻').click()
yun_x=372
yun_y=1524

#glaxy_x=yun_x*gallery*full/cuizi_full
time.sleep(15)
d.click(yun_x,yun_y)
time.sleep(10)

daka_x=displayWidth/2
daka_y=displayHeight/2
d.click(daka_x,daka_y)
time.sleep(20)
print "Sunning Done"

#淘宝的金币
def taobao_cuizi():
d.screen.on()
d.press.home()
#解锁,没有密码的情况下
'''
sx=560
sy=1700
ex=560
ey=900
#d.swipe(sx,sy,ex,ey,steps=2)

#d(scrollable=True).fling.horiz.forward()
home_swipe_sx=950
home_swipe_sy=1350
home_swipe_ex=450
home_swipe_ey=1350
while not d(text=u"手机淘宝").exists:
d.swipe(home_swipe_sx,home_swipe_sy,home_swipe_ex,home_swipe_ey,steps=2)
time.sleep(3)
d(text=u'手机淘宝').click()
'''
activity_name='com.taobao.taobao/com.taobao.tao.homepage.MainActivity3'
launch_app(activity_name)
if d(text=u'领金币').wait.exists(timeout=12*1000):
#print "Dismiss update"
d(text=u'领金币').click()
#登录账号,刷新下即可

time.sleep(15)
jb_x=900
jb_y=370
d.click(jb_x,jb_y)

完整代码:
https://github.com/Rockyzsu/red_bag
 

微信自动回复 微信小助手

李魔佛 发表了文章 • 0 个评论 • 6116 次浏览 • 2017-02-04 15:30 • 来自相关话题

无意中发现itchat这个库,python太牛了,只有想不到,没有做不到哈。
 
用法很简单。
 
#-*-coding=utf-8-*-
import itchat

@itchat.msg_register(itchat.content.TEXT)
def text_reply(msg):
reply_msg=u'新年快乐! 我是xxx的小秘书,你的消息已收到,主人正忙,稍后会回复你哦~'
return reply_msg

itchat.auto_login(hotReload=True)
itchat.run()
然后运行上面的python文件,用自己的微信扫码登录就可以了。
只要别人发微信给你,对方就可以收到你的自动回复的内容。
 
  查看全部
无意中发现itchat这个库,python太牛了,只有想不到,没有做不到哈。
 
用法很简单。
 
#-*-coding=utf-8-*-
import itchat

@itchat.msg_register(itchat.content.TEXT)
def text_reply(msg):
reply_msg=u'新年快乐! 我是xxx的小秘书,你的消息已收到,主人正忙,稍后会回复你哦~'
return reply_msg

itchat.auto_login(hotReload=True)
itchat.run()

然后运行上面的python文件,用自己的微信扫码登录就可以了。
只要别人发微信给你,对方就可以收到你的自动回复的内容。
 
 

使用android系统对wifi密码进行枚举破解

李魔佛 发表了文章 • 0 个评论 • 4053 次浏览 • 2017-01-25 12:31 • 来自相关话题

 
ETA 1.28
Link: https://github.com/Rockyzsu/crack_wifi_by_android
 

自动抢雪球红包 python代码

python爬虫李魔佛 发表了文章 • 0 个评论 • 17035 次浏览 • 2017-01-25 12:29 • 来自相关话题

ETA 1.30
Link https://github.com/Rockyzsu/red_bag​ 
 
 

pyautogui 在Windows下遇到 WindowsError: [Error 5] Access is denied. 错误

回复

李魔佛 发起了问题 • 1 人关注 • 0 个回复 • 5818 次浏览 • 2017-01-16 02:03 • 来自相关话题

python爬虫出现问题的调试真的好耗时间呀

李魔佛 发表了文章 • 3 个评论 • 3377 次浏览 • 2017-01-08 01:44 • 来自相关话题

不同的异常都要调试。 而且每次异常都不一样。
不同的异常都要调试。 而且每次异常都不一样。

ubuntu python安装MySQL (MySQLdb)

李魔佛 发表了文章 • 0 个评论 • 3444 次浏览 • 2016-12-29 17:53 • 来自相关话题

首先安装mysql数据库
sudo apt-get install mysql-server
设置好用户密码

然后安装pyMySQLdb,使用pip安装最方便。

pip install MySQL-python

如果遇到错误:
EnvironmentError: mysql_config not found

说明没找到配置文件, 需要安装:
libmysqlclient-dev

安装命令:

sudo apt-get install libmysqlclient-dev

安装完成之后在python命令行中输入

import MySQLdb

没有出错的话就说明安装成功了。



  查看全部
首先安装mysql数据库
sudo apt-get install mysql-server
设置好用户密码

然后安装pyMySQLdb,使用pip安装最方便。

pip install MySQL-python

如果遇到错误:
EnvironmentError: mysql_config not found

说明没找到配置文件, 需要安装:
libmysqlclient-dev

安装命令:

sudo apt-get install libmysqlclient-dev

安装完成之后在python命令行中输入

import MySQLdb

没有出错的话就说明安装成功了。



 

python NoneType的判断

李魔佛 发表了文章 • 0 个评论 • 5749 次浏览 • 2016-10-22 15:26 • 来自相关话题

比如在爬虫过程中 
content = urllib2.urlopen("http://www.qq1.com").read() 
title=bs.title.string.strip()
 
上面由于网址写错了, 那么title的值如果为NoneType (不同于null 类型) 
那么 需要用的判断和null不一样
 
if title is None:
 print "No title"
 
这样就可以避免 title哪里出错。
(TypeError: object of type 'NoneType' has no len()
或者
TypeError: object of type 'NoneType' has no strip()
查看全部
比如在爬虫过程中 
content = urllib2.urlopen("http://www.qq1.com").read() 
title=bs.title.string.strip()
 
上面由于网址写错了, 那么title的值如果为NoneType (不同于null 类型) 
那么 需要用的判断和null不一样
 
if title is None:
 print "No title"
 
这样就可以避免 title哪里出错。
(TypeError: object of type 'NoneType' has no len()
或者
TypeError: object of type 'NoneType' has no strip()

每天自动获取深圳上海北京的新房二手房的成交量

python爬虫李魔佛 发表了文章 • 2 个评论 • 17881 次浏览 • 2016-10-10 14:28 • 来自相关话题

静观其变,目前的态势不宜参与进去。

每天自动获取深圳上海北京的新房二手房的成交量
深圳市房地产信息系统:http://ris.szpl.gov.cn/
 





 




#-*-coding=utf-8-*-
__author__ = 'rocky'
#获取每天深圳一手房,二手房的成交套数与面积,并且写入数据库
#主要就是正则表达抓取几个数字
import urllib2,re
import database
def getContent():
url="http://ris.szpl.gov.cn/"
one_hand="credit/showcjgs/ysfcjgs.aspx"
second_hand="credit/showcjgs/esfcjgs.aspx"
req=urllib2.Request(url+one_hand)
content=urllib2.urlopen(req).read()
#返回的就是网页的源码,没有做任何防爬虫的处理,zf网站,呵呵
#print content
date=re.compile(r'<SPAN class=titleblue><span id=\"lblCurTime5\">(.*)</span>')
reg=re.compile(r'<td width="14%"><b>(\d+)</b>')
result=reg.findall(content)
current_date=date.findall(content)

reg2=re.compile(r'<td align="right"><b>(.*?)</b>')
yishou_area=reg2.findall(content)


print current_date[0]
print '一手商品房成交套数:%s' % result[0]
print '一手商品房成交面积: %s' % yishou_area[0]


sec_req=urllib2.Request(url+second_hand)
sec_content=urllib2.urlopen(sec_req).read()

sec_quantity=re.compile(r'<td width="30%">(\d+)</td>')
sec_result=sec_quantity.findall(sec_content)
second_area=re.findall(r'<td align="right">(.*?)</td>',sec_content)

print '二手商品房成交套数:%s' % sec_result[1]
print '二手商品房成交面积: %s' % second_area[2]
database.create_table()
database.insert(current_date[0],result[0],yishou_area[0],sec_result[1],second_area[2])

getContent()
 github代码:https://github.com/Rockyzsu/house​
 

  查看全部
静观其变,目前的态势不宜参与进去。

每天自动获取深圳上海北京的新房二手房的成交量
深圳市房地产信息系统:http://ris.szpl.gov.cn/
 

一手.PNG

 

house.PNG
#-*-coding=utf-8-*-
__author__ = 'rocky'
#获取每天深圳一手房,二手房的成交套数与面积,并且写入数据库
#主要就是正则表达抓取几个数字
import urllib2,re
import database
def getContent():
url="http://ris.szpl.gov.cn/"
one_hand="credit/showcjgs/ysfcjgs.aspx"
second_hand="credit/showcjgs/esfcjgs.aspx"
req=urllib2.Request(url+one_hand)
content=urllib2.urlopen(req).read()
#返回的就是网页的源码,没有做任何防爬虫的处理,zf网站,呵呵
#print content
date=re.compile(r'<SPAN class=titleblue><span id=\"lblCurTime5\">(.*)</span>')
reg=re.compile(r'<td width="14%"><b>(\d+)</b>')
result=reg.findall(content)
current_date=date.findall(content)

reg2=re.compile(r'<td align="right"><b>(.*?)</b>')
yishou_area=reg2.findall(content)


print current_date[0]
print '一手商品房成交套数:%s' % result[0]
print '一手商品房成交面积: %s' % yishou_area[0]


sec_req=urllib2.Request(url+second_hand)
sec_content=urllib2.urlopen(sec_req).read()

sec_quantity=re.compile(r'<td width="30%">(\d+)</td>')
sec_result=sec_quantity.findall(sec_content)
second_area=re.findall(r'<td align="right">(.*?)</td>',sec_content)

print '二手商品房成交套数:%s' % sec_result[1]
print '二手商品房成交面积: %s' % second_area[2]
database.create_table()
database.insert(current_date[0],result[0],yishou_area[0],sec_result[1],second_area[2])

getContent()

 github代码:https://github.com/Rockyzsu/house​
 

 

深圳汽车摇号系统的登录验证码 就是一坨垃圾学生做的

李魔佛 发表了文章 • 0 个评论 • 5525 次浏览 • 2016-10-05 23:46 • 来自相关话题

每次填完一次就自动更新,时间能不能慢点呀? 所以每次填入验证码都是提示错误。
 
网站是学生做的,漏洞百出,垃圾中的战斗机。
每次填完一次就自动更新,时间能不能慢点呀? 所以每次填入验证码都是提示错误。
 
网站是学生做的,漏洞百出,垃圾中的战斗机。

python中的 if __name__ == __main__ 语句

李魔佛 发表了文章 • 0 个评论 • 2609 次浏览 • 2016-08-16 17:24 • 来自相关话题

python中 的

if "__name__" == "__main__" :

    不一定会在开头执行, 因为前面还有语句呢。
比如:

print "Hello"
if "__name__"=="__main__":
    print "Main"

这个就会打印
Hello
Mian
  查看全部

python中 的

if "__name__" == "__main__" :

    不一定会在开头执行, 因为前面还有语句呢。
比如:

print "Hello"
if "__name__"=="__main__":
    print "Main"

这个就会打印
Hello
Mian
 

使用requests 访问https的网页 返回错误: InsecurePlatformWarning: A true SSLContext object is not available

回复

李魔佛 回复了问题 • 1 人关注 • 1 个回复 • 9439 次浏览 • 2016-08-13 22:52 • 来自相关话题

python 爬虫获取XiciDaili代理IP

python爬虫李魔佛 发表了文章 • 20 个评论 • 22587 次浏览 • 2016-08-11 23:17 • 来自相关话题

默认获取前5页的代理IP,验证其是否有效,然后使用sqlite存储为本地db文件。

 
 




 class getProxy():

def __init__(self):
self.user_agent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)"
self.header = {"User-Agent": self.user_agent}
self.dbname="proxy.db"
self.now = time.strftime("%Y-%m-%d")

def getContent(self, num):
nn_url = "http://www.xicidaili.com/nn/" + str(num)
#国内高匿
req = urllib2.Request(nn_url, headers=self.header)
resp = urllib2.urlopen(req, timeout=10)
content = resp.read()
et = etree.HTML(content)
result_even = et.xpath('//tr[@class=""]')
result_odd = et.xpath('//tr[@class="odd"]')
#因为网页源码中class 分开了奇偶两个class,所以使用lxml最方便的方式就是分开获取。
#刚开始我使用一个方式获取,因而出现很多不对称的情况,估计是网站会经常修改源码,怕被其他爬虫的抓到
#使用上面的方法可以不管网页怎么改,都可以抓到ip 和port
for i in result_even:
t1 = i.xpath("./td/text()")[:2]
print "IP:%s\tPort:%s" % (t1[0], t1[1])
if self.isAlive(t1[0], t1[1]):

self.insert_db(self.now,t1[0],t1[1])
for i in result_odd:
t2 = i.xpath("./td/text()")[:2]
print "IP:%s\tPort:%s" % (t2[0], t2[1])
if self.isAlive(t2[0], t2[1]):
self.insert_db(self.now,t2[0],t2[1])
接着实现写插入数据库函数:def insert_db(self,date,ip,port):
dbname=self.dbname
try:
conn=sqlite3.connect(dbname)
except:
print "Error to open database%" %self.dbname
create_tb='''
CREATE TABLE IF NOT EXISTS PROXY
(DATE TEXT,
IP TEXT,
PORT TEXT
);
'''
conn.execute(create_tb)
insert_db_cmd='''
INSERT INTO PROXY (DATE,IP,PORT) VALUES ('%s','%s','%s');
''' %(date,ip,port) #写入时间,ip和端口
conn.execute(insert_db_cmd)
conn.commit() #记得commit
conn.close()





接着完成判断代理是否有效 #查看爬到的代理IP是否还能用
def isAlive(self,ip,port):
proxy={'http':ip+':'+port}
print proxy

#使用这个方式是全局方法。
proxy_support=urllib2.ProxyHandler(proxy)
opener=urllib2.build_opener(proxy_support)
urllib2.install_opener(opener)
#使用代理访问腾讯官网,进行验证代理是否有效
test_url="http://www.qq.com"
req=urllib2.Request(test_url,headers=self.header)
try:
#timeout 设置为10,如果你不能忍受你的代理延时超过10,就修改timeout的数字
resp=urllib2.urlopen(req,timeout=10)

if resp.code==200:
print "work"
return True
else:
print "not work"
return False
except :
print "Not work"
return False
 获取前面多少也的代理IP,用一个循环即可: def loop(self,page):
for i in range(1,page):
self.getContent(i)
更新2016-08-13
接着实现对已有的数据库进行清洗,失效的代理要移除。 待续。。。
 
 
 
调用类实例:设置爬取前面5页的代理ipif __name__ == "__main__":
now = datetime.datetime.now()
print "Start at %s" % now
obj=getProxy()
obj.loop(5)


获取最新source code,可以到
https://github.com/Rockyzsu/getProxy 
sync up 查看全部
默认获取前5页的代理IP,验证其是否有效,然后使用sqlite存储为本地db文件。

 
 

proxy.PNG
 
class getProxy():

def __init__(self):
self.user_agent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)"
self.header = {"User-Agent": self.user_agent}
self.dbname="proxy.db"
self.now = time.strftime("%Y-%m-%d")

def getContent(self, num):
nn_url = "http://www.xicidaili.com/nn/" + str(num)
#国内高匿
req = urllib2.Request(nn_url, headers=self.header)
resp = urllib2.urlopen(req, timeout=10)
content = resp.read()
et = etree.HTML(content)
result_even = et.xpath('//tr[@class=""]')
result_odd = et.xpath('//tr[@class="odd"]')
#因为网页源码中class 分开了奇偶两个class,所以使用lxml最方便的方式就是分开获取。
#刚开始我使用一个方式获取,因而出现很多不对称的情况,估计是网站会经常修改源码,怕被其他爬虫的抓到
#使用上面的方法可以不管网页怎么改,都可以抓到ip 和port
for i in result_even:
t1 = i.xpath("./td/text()")[:2]
print "IP:%s\tPort:%s" % (t1[0], t1[1])
if self.isAlive(t1[0], t1[1]):

self.insert_db(self.now,t1[0],t1[1])
for i in result_odd:
t2 = i.xpath("./td/text()")[:2]
print "IP:%s\tPort:%s" % (t2[0], t2[1])
if self.isAlive(t2[0], t2[1]):
self.insert_db(self.now,t2[0],t2[1])

接着实现写插入数据库函数:
def insert_db(self,date,ip,port):
dbname=self.dbname
try:
conn=sqlite3.connect(dbname)
except:
print "Error to open database%" %self.dbname
create_tb='''
CREATE TABLE IF NOT EXISTS PROXY
(DATE TEXT,
IP TEXT,
PORT TEXT
);
'''
conn.execute(create_tb)
insert_db_cmd='''
INSERT INTO PROXY (DATE,IP,PORT) VALUES ('%s','%s','%s');
''' %(date,ip,port) #写入时间,ip和端口
conn.execute(insert_db_cmd)
conn.commit() #记得commit
conn.close()





接着完成判断代理是否有效
 #查看爬到的代理IP是否还能用
def isAlive(self,ip,port):
proxy={'http':ip+':'+port}
print proxy

#使用这个方式是全局方法。
proxy_support=urllib2.ProxyHandler(proxy)
opener=urllib2.build_opener(proxy_support)
urllib2.install_opener(opener)
#使用代理访问腾讯官网,进行验证代理是否有效
test_url="http://www.qq.com"
req=urllib2.Request(test_url,headers=self.header)
try:
#timeout 设置为10,如果你不能忍受你的代理延时超过10,就修改timeout的数字
resp=urllib2.urlopen(req,timeout=10)

if resp.code==200:
print "work"
return True
else:
print "not work"
return False
except :
print "Not work"
return False

 获取前面多少也的代理IP,用一个循环即可:
    def loop(self,page):
for i in range(1,page):
self.getContent(i)

更新2016-08-13
接着实现对已有的数据库进行清洗,失效的代理要移除。 待续。。。
 
 
 
调用类实例:设置爬取前面5页的代理ip
if __name__ == "__main__":
now = datetime.datetime.now()
print "Start at %s" % now
obj=getProxy()
obj.loop(5)



获取最新source code,可以到
https://github.com/Rockyzsu/getProxy 
sync up

python 判断sqlite数据库中的表是否存在,不存在就创建

李魔佛 发表了文章 • 0 个评论 • 31144 次浏览 • 2016-08-11 22:26 • 来自相关话题

#判断表存不存在来创建表
def create_table():

conn = sqlite3.connect(db_name)
try:
create_tb_cmd='''
CREATE TABLE IF NOT EXISTS USER
(NAME TEXT,
AGE INT,
SALARY REAL);
'''
#主要就是上面的语句
conn.execute(create_tb_cmd)
except:
print "Create table failed"
return False
insert_dt_cmd='''
INSERT INTO USER (NAME,AGE,SALARY) VALUES ("Jack",10,20.1);
'''
conn.execute(insert_dt_cmd)
conn.commit()
conn.close() 
代码如上,主要就是 
CREATE TABLE IF NOT EXISTS USER 查看全部
#判断表存不存在来创建表
def create_table():

conn = sqlite3.connect(db_name)
try:
create_tb_cmd='''
CREATE TABLE IF NOT EXISTS USER
(NAME TEXT,
AGE INT,
SALARY REAL);
'''
#主要就是上面的语句
conn.execute(create_tb_cmd)
except:
print "Create table failed"
return False
insert_dt_cmd='''
INSERT INTO USER (NAME,AGE,SALARY) VALUES ("Jack",10,20.1);
'''
conn.execute(insert_dt_cmd)
conn.commit()
conn.close()
 
代码如上,主要就是 
CREATE TABLE IF NOT EXISTS USER

datetime weekday (可以返回某天是一个星期的第几天)的源码只有return 0

回复

李魔佛 回复了问题 • 1 人关注 • 1 个回复 • 5917 次浏览 • 2016-08-07 17:57 • 来自相关话题

python @classmethod 的使用场合

李魔佛 发表了文章 • 0 个评论 • 13018 次浏览 • 2016-08-07 11:01 • 来自相关话题

官方的说法: 
classmethod(function)
中文说明:
classmethod是用来指定一个类的方法为类方法,没有此参数指定的类的方法为实例方法,使用方法如下:class C:
@classmethod
def f(cls, arg1, arg2, ...): ...
 
 看后之后真是一头雾水。说的啥子东西呢???
 
自己到国外的论坛看其他的例子和解释,顿时就很明朗。 下面自己用例子来说明。
 
看下面的定义的一个时间类:class Data_test(object):
day=0
month=0
year=0
def __init__(self,year=0,month=0,day=0):
self.day=day
self.month=month
self.year=year

def out_date(self):
print "year :"
print self.year
print "month :"
print self.month
print "day :"
print self.day



t=Data_test(2016,8,1)
t.out_date()




输出: year :
2016
month :
8
day :
1
符合期望。
 
如果用户输入的是 "2016-8-1" 这样的字符格式,那么就需要调用Date_test 类前做一下处理:string_date='2016-8-1'
year,month,day=map(int,string_date.split('-'))
s=Data_test(year,month,day)
先把‘2016-8-1’ 分解成 year,month,day 三个变量,然后转成int,再调用Date_test(year,month,day)函数。 也很符合期望。
 
那我可不可以把这个字符串处理的函数放到 Date_test 类当中呢?
 
那么@classmethod 就开始出场了class Data_test2(object):
day=0
month=0
year=0
def __init__(self,year=0,month=0,day=0):
self.day=day
self.month=month
self.year=year

@classmethod
def get_date(cls,
string_date):
#这里第一个参数是cls, 表示调用当前的类名
year,month,day=map(int,string_date.split('-'))
date1=cls(year,month,day)
#返回的是一个初始化后的类
return date1

def out_date(self):
print "year :"
print self.year
print "month :"
print self.month
print "day :"
print self.day
在Date_test类里面创建一个成员函数, 前面用了@classmethod装饰。 它的作用就是有点像静态类,比静态类不一样的就是它可以传进来一个当前类作为第一个参数。
 
那么如何调用呢?r=Data_test2.get_date("2016-8-6")
r.out_date()输出:year :
2016
month :
8
day :
1
这样子等于先调用get_date()对字符串进行处理,然后才使用Data_test的构造函数初始化。
 
这样的好处就是你以后重构类的时候不必要修改构造函数,只需要额外添加你要处理的函数,然后使用装饰符 @classmethod 就可以了。
 
 
本文原创
转载请注明出处:http://30daydo.com/article/89
  查看全部
官方的说法: 
classmethod(function)
中文说明:
classmethod是用来指定一个类的方法为类方法,没有此参数指定的类的方法为实例方法,使用方法如下:
class C:
@classmethod
def f(cls, arg1, arg2, ...): ...

 
 看后之后真是一头雾水。说的啥子东西呢???
 
自己到国外的论坛看其他的例子和解释,顿时就很明朗。 下面自己用例子来说明。
 
看下面的定义的一个时间类:
class Data_test(object):
day=0
month=0
year=0
def __init__(self,year=0,month=0,day=0):
self.day=day
self.month=month
self.year=year

def out_date(self):
print "year :"
print self.year
print "month :"
print self.month
print "day :"
print self.day



t=Data_test(2016,8,1)
t.out_date()




输出: 
year :
2016
month :
8
day :
1

符合期望。
 
如果用户输入的是 "2016-8-1" 这样的字符格式,那么就需要调用Date_test 类前做一下处理:
string_date='2016-8-1'
year,month,day=map(int,string_date.split('-'))
s=Data_test(year,month,day)

先把‘2016-8-1’ 分解成 year,month,day 三个变量,然后转成int,再调用Date_test(year,month,day)函数。 也很符合期望。
 
那我可不可以把这个字符串处理的函数放到 Date_test 类当中呢?
 
那么@classmethod 就开始出场了
class Data_test2(object):
day=0
month=0
year=0
def __init__(self,year=0,month=0,day=0):
self.day=day
self.month=month
self.year=year

@classmethod
def get_date(cls,
string_date):
#这里第一个参数是cls, 表示调用当前的类名
year,month,day=map(int,string_date.split('-'))
date1=cls(year,month,day)
#返回的是一个初始化后的类
return date1

def out_date(self):
print "year :"
print self.year
print "month :"
print self.month
print "day :"
print self.day

在Date_test类里面创建一个成员函数, 前面用了@classmethod装饰。 它的作用就是有点像静态类,比静态类不一样的就是它可以传进来一个当前类作为第一个参数。
 
那么如何调用呢?
r=Data_test2.get_date("2016-8-6")
r.out_date()
输出:
year :
2016
month :
8
day :
1

这样子等于先调用get_date()对字符串进行处理,然后才使用Data_test的构造函数初始化。
 
这样的好处就是你以后重构类的时候不必要修改构造函数,只需要额外添加你要处理的函数,然后使用装饰符 @classmethod 就可以了。
 
 
本文原创
转载请注明出处:http://30daydo.com/article/89
 

怎么segmentfault上的问题都这么入门级别的?

李魔佛 发表了文章 • 0 个评论 • 2287 次浏览 • 2016-07-28 16:37 • 来自相关话题

遇到一些问题,上去segmentfault上搜索答案,以为segmentfault是中文版的stackoverflow。 结果大失所望。
基本都是一些菜鸟的问题。
 
搜索关键字: python
出来的是





 
结果都是怎么安装python,选择python2还是python3 这一类的问题。 着实无语。
看来在中国肯义务分享技术的人并不像国外那么多,那么慷慨。
(也有可能大神们都在忙于做项目,没空帮助小白们吧) 查看全部
遇到一些问题,上去segmentfault上搜索答案,以为segmentfault是中文版的stackoverflow。 结果大失所望。
基本都是一些菜鸟的问题。
 
搜索关键字: python
出来的是

segenfault.PNG

 
结果都是怎么安装python,选择python2还是python3 这一类的问题。 着实无语。
看来在中国肯义务分享技术的人并不像国外那么多,那么慷慨。
(也有可能大神们都在忙于做项目,没空帮助小白们吧)

AttributeError: 'module' object has no attribute 'pyplot'

回复

李魔佛 回复了问题 • 1 人关注 • 1 个回复 • 9051 次浏览 • 2016-07-28 12:31 • 来自相关话题