在python中可以用id()函數(shù)獲取對(duì)象的內(nèi)存地址。
創(chuàng)新互聯(lián)從2013年創(chuàng)立,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè)網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元成縣做網(wǎng)站,已為上家服務(wù),為成縣各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:18980820575
#例如:
object = 1 + 2
print(id(object)) #4304947776
如果你想通過(guò)函數(shù)的名稱來(lái)獲取函數(shù)的運(yùn)行地址,可以像下面這樣實(shí)現(xiàn):
[python]?view plain?copy
#?File:?builtin-import-example-2.py
def?getfunctionbyname(module_name,?function_name):
module?=?__import__(module_name)
return?getattr(module,?function_name)
print(?repr(getfunctionbyname("dbm",?"open"))?)
在這段代碼里,通過(guò)open字符串的名稱獲取數(shù)據(jù)庫(kù)管理模塊dbm的open函數(shù)地址。
運(yùn)行之后,輸出如下:
=== RESTART: D:\work\csdn\python_Game1\example\builtin-import-example-2.py ===
function open at 0x00000226467B2BF8
1、print()函數(shù):打印字符串;
2、raw_input()函數(shù):從用戶鍵盤(pán)捕獲字符;
3、len()函數(shù):計(jì)算字符長(zhǎng)度;
4、format()函數(shù):實(shí)現(xiàn)格式化輸出;
5、type()函數(shù):查詢對(duì)象的類(lèi)型;
6、int()函數(shù)、float()函數(shù)、str()函數(shù)等:類(lèi)型的轉(zhuǎn)化函數(shù);
7、id()函數(shù):獲取對(duì)象的內(nèi)存地址;
8、help()函數(shù):Python的幫助函數(shù);
9、s.islower()函數(shù):判斷字符小寫(xiě);
10、s.sppace()函數(shù):判斷是否為空格;
11、str.replace()函數(shù):替換字符;
12、import()函數(shù):引進(jìn)庫(kù);
13、math.sin()函數(shù):sin()函數(shù);
14、math.pow()函數(shù):計(jì)算次方函數(shù);
15、os.getcwd()函數(shù):獲取當(dāng)前工作目錄;
16、listdir()函數(shù):顯示當(dāng)前目錄下的文件;
17、time.sleep()函數(shù):停止一段時(shí)間;
18、random.randint()函數(shù):產(chǎn)生隨機(jī)數(shù);
19、range()函數(shù):返回一個(gè)列表,打印從1到100;
20、file.read()函數(shù):讀取文件返回字符串;
21、file.readlines()函數(shù):讀取文件返回列表;
22、file.readline()函數(shù):讀取一行文件并返回字符串;
23、split()函數(shù):用什么來(lái)間隔字符串;
24、isalnum()函數(shù):判斷是否為有效數(shù)字或字符;
25、isalpha()函數(shù):判斷是否全為字符;
26、isdigit()函數(shù):判斷是否全為數(shù)字;
27、 lower()函數(shù):將數(shù)據(jù)改成小寫(xiě);
28、upper()函數(shù):將數(shù)據(jù)改成大寫(xiě);
29、startswith(s)函數(shù):判斷字符串是否以s開(kāi)始的;
30、endwith(s)函數(shù):判斷字符串是否以s結(jié)尾的;
31、file.write()函數(shù):寫(xiě)入函數(shù);
32、file.writeline()函數(shù):寫(xiě)入文件;
33、abs()函數(shù):得到某數(shù)的絕對(duì)值;
34、file.sort()函數(shù):對(duì)書(shū)數(shù)據(jù)排序;
35、tuple()函數(shù):創(chuàng)建一個(gè)元組;
36、find()函數(shù):查找 返回的是索引;
37、dict()函數(shù):創(chuàng)建字典;
38、clear()函數(shù):清楚字典中的所有項(xiàng);
39、copy()函數(shù):復(fù)制一個(gè)字典,會(huì)修改所有的字典;
40、 get()函數(shù):查詢字典中的元素。
…………
Copyright ? 1999-2020, CSDN.NET, All Rights Reserved
python
打開(kāi)APP
pergoods
關(guān)注
Python多線程爬取網(wǎng)站image的src屬性實(shí)例 原創(chuàng)
2017-05-16 11:18:51
pergoods
碼齡6年
關(guān)注
# coding=utf-8
'''
Created on 2017年5月16日
@author: chenkai
Python多線程爬取某單無(wú)聊圖圖片地址(requests+BeautifulSoup+threading+Queue模塊)
'''
import requests
from bs4 import BeautifulSoup
import threading
import Queue
import time
class Spider_Test(threading.Thread):
def __init__(self,queue):
threading.Thread.__init__(self)
self.__queue = queue
def run(self):
while not self.__queue.empty():
page_url=self.__queue.get() [color=red]#從隊(duì)列中取出url[/color]
print page_url
self.spider(page_url)
def spider(self,url):
r=requests.get(url) [color=red]#請(qǐng)求url[/color]
soup=BeautifulSoup(r.content,'lxml') [color=red]#r.content就是響應(yīng)內(nèi)容,轉(zhuǎn)換為lxml的bs對(duì)象[/color]
imgs = soup.find_all(name='img',attrs={}) #查找所有的img標(biāo)簽,并獲取標(biāo)簽屬性值(為列表類(lèi)型)
for img in imgs:
if 'onload' in str(img): [color=red]#img屬性集合中包含onload屬性的為動(dòng)態(tài)圖.gif,[/color]
print 'http:'+img['org_src']
else:
print 'http:'+img['src']
def main():
queue=Queue.Queue()
url_start = '-'
for i in range(293,295):
url = url_start+str(i)+'#comment'
queue.put(url) [color=red]#將循環(huán)拼接的url放入隊(duì)列中[/color]
threads=[]
thread_count=2 [color=red]#默認(rèn)線程數(shù)(可自動(dòng)修改)[/color]
for i in range(thread_count):
threads.append(Spider_Test(queue))
for i in threads:
i.start()
for i in threads:
i.join()
if __name__ == '__main__':[color=red] #在.py文件中使用這個(gè)條件語(yǔ)句,可以使這個(gè)條件語(yǔ)句塊中的命令只在它獨(dú)立運(yùn)行時(shí)才執(zhí)行[/color]
time_start = time.time()
main() [color=red]#調(diào)用main方法[/color]
print time.time()-time_start
[color=red]#背景知識(shí)[/color]
'''
q = Queue.Queue(maxsize = 10)
Queue.Queue類(lèi)即是一個(gè)隊(duì)列的同步實(shí)現(xiàn)。隊(duì)列長(zhǎng)度可為無(wú)限或者有限。可通過(guò)Queue的構(gòu)造函數(shù)的可選參數(shù)maxsize來(lái)設(shè)定隊(duì)列長(zhǎng)度。如果maxsize小于1就表示隊(duì)列長(zhǎng)度無(wú)限。
將一個(gè)值放入隊(duì)列中
q.put(10)
調(diào)用隊(duì)列對(duì)象的put()方法在隊(duì)尾插入一個(gè)項(xiàng)目。put()有兩個(gè)參數(shù),第一個(gè)item為必需的,為插入項(xiàng)目的值;第二個(gè)block為可選參數(shù),默認(rèn)為
1。如果隊(duì)列當(dāng)前為空且block為1,put()方法就使調(diào)用線程暫停,直到空出一個(gè)數(shù)據(jù)單元。如果block為0,put方法將引發(fā)Full異常。
將一個(gè)值從隊(duì)列中取出
q.get()
調(diào)用隊(duì)列對(duì)象的get()方法從隊(duì)頭刪除并返回一個(gè)項(xiàng)目??蛇x參數(shù)為block,默認(rèn)為T(mén)rue。如果隊(duì)列為空且block為T(mén)rue,get()就使調(diào)用線程暫停,直至有項(xiàng)目可用。如果隊(duì)列為空且block為False,隊(duì)列將引發(fā)Empty異常。
'''
[color=red]如果想要下載圖片需要
import urllib
再替換spider方法即可[/color]
def spider(self,url):
r=requests.get(url)
soup=BeautifulSoup(r.content,'lxml')
imgs = soup.find_all(name='img',attrs={})
urls=[]
for img in imgs:
if 'onload' in str(img):
print 'http:'+img['org_src']
urls.append('http:'+img['org_src'])
else:
print 'http:'+img['src']
url = urls.append('http:'+img['src'])
#下載圖片
k=0
for urlitem in urls:
k+=1
if '.jpg' in urlitem:
urllib.urlretrieve(url=urlitem,filename='F:\image\\'+str(k)+'.jpg')
[color=red]-----------多線程訪問(wèn)百度實(shí)例[/color]
#coding:utf-8
import requests
import threading
import time
import sys
url = ''
def get_baidu():
global url
time_start = time.time()
r = requests.get(url=url)
times = time.time()-time_start
sys.stdout.write('status:%s time:%s current_time:%s\n'%(r.status_code,times,time.strftime('%H:%M:%S')))
def main():
threads = []
thread_count = 10
for i in range(thread_count):
t = threading.Thread(target=get_baidu,args=())
threads.append(t)
for i in range(thread_count):
threads[i].start()
for i in range(thread_count):
threads[i].join()
if __name__=='__main__':