Statement commands commonly used in crawlers in python

One, format application statement

print.format command

① Use class without parameters

print('{},{},{}'.format('zhangk','boy',32))#(即参数与()对齐类)
#即参数几个,即几个括号进行打印的状况特点
print('{:.3%}'.format(i/count)+'  '+chaptername[i])#利用print的输出格式思路类代写

②Type with parameters Type
with format characters
Common two kinds of symbols
# ^, <,> are centered, left-justified, right-justified, followed by width
#: The character with padding after the sign, which can only be one character, default is not specified Is filled with spaces
such as

	print('{:a<8}'.format('zhang'))#zhangaaa即使用a填充
	print('{:p^10}'.format('zhang'))#ppzhangppp

③Precision sentence class
, that is, the thought class of controlling precision

print('{:.2f}'.format(31.31412))#31.31

Binary combination type The
combination type
b, d, o and x are binary, decimal, octal, hexadecimal (that is, the meaning of converting the value to a specific hexadecimal)

		print('{:b}'.format(15))#1111
		print('{:d}'.format(15))#15
		print('{:o}'.format(15))#17
		print('{:x}'.format(15))#f

Second, the application statement class

  1. The enumeration statement enumerate()
    is to use loop traversal to enumerate the effect of parameters
>>>seq = ['one', 'two', 'three']
>>>for i, element in enumerate(seq):#即第一个参数是序列号的含义
        print i, element
#0 one
#1 two
#2 three
  1. Pass parameter function statement get_translate_data (parameter=None)
#传参函数
get_translate_data(word=None)#注意该word参数是网页中参数的状况特点
input()#利用input命令传参

3. Set the main function command (that is, determine whether to choose the status of execution when the script is executed)

if _name_=='__main__'

4. Truncate commands (typically used to name classes)

 x=url.split('/')[-1]#只选url/后的最后一部分思路状况特点

5. Add a value at the end (usually used to assign a value to a variable)
Syntax: list.append(obj)

aList = [123, 'xyz', 'zara', 'abc'];
aList.append( 2009 );
print "Updated List : ", aList;
#结果=Updated List :  [123, 'xyz', 'zara', 'abc', 2009]
#也可以利用语句相加的模式代
#如content='    '
#p='abc'
#content+=p.string,即可代加上p的值

Guess you like

Origin blog.csdn.net/qq_33942040/article/details/108381368