Python爬虫基础语法相关
1. if条件判断
if 条件:
# 事情1
else:
# 事情2
2. while循环
while 条件:
循环体
3. 关于True和False
# 几乎所有能表示为空的东西. 都可以认为是False
print(bool(0))
print(bool(""))
print(bool([]))
print(bool({}))
print(bool(set()))
print(bool(tuple()))
print(bool(None))
4. 字符串
A. 索引和切片
索引, 就是第几个字符. 它从0开始.
切片, 从字符串中提取n个字符.
s = "我爱你"
print(s[1]) # 爱
print(s[0]) # 我
print(s[2:4]) #从第2个, 到第4个(取不到4) --你
B. strip()
strip()可以去掉字符串`左右两端`的空白(空格, 换行\n, 回车\r, 制表符\t)
s = " \t\t\t 是的士大夫为我\r\r \n\n " # 够乱的字符串
s1 = s.strip()
print(s1) # 是的士大夫为我
C. split()
做切割的
s = "10,男人本色,100000万" # 你在网页上提取到这样的一段数据. 现在我需要电影名称
tmps = s.split(",")
name = tmps[1]
print(name) # 男人本色
id, name, money = s.split(",") # 切割后. 把三个结果直接怼给三个变量
print(id)
print(name)
print(money)
D. replace()
replace, 字符串替换
s = "的 \t\t\n\n属地往 32 师弟 "
s1 = replace(" ", "").replace("\t", "").replace("\n", "")
E. join()
lst = ["我妈", "不喜欢", "黎明"] # 有时,由于网页结构的不规则, 导致获取的数据是这样的.
s1 = "".join(lst) # 用空字符串把lst中的每一项拼接起来
print(s1) # 我妈不喜欢黎明
lst2 = ["\n\r","\n\r",&