x = '1q2wwyxliuwyx3e4rwyxywshunwyx1q2wwyxxiangwyx3e4r'
y = re.findall('wyx.',x) # 可以将点号(.)理解为一个占位符,而且这个占位符可以代表一切字符
print(y)
y = re.findall('wyx...',x)
print(y)
y = re.findall('wyx*',x) # 星号(*)前一个字符为x,所以返回结果中可以找到x的任意次
print(y)
y = re.findall('wyx?',x) # 匹配前一个字符0次或1次,与星号的不同在于其最多匹配一次
print(y)
y = re.findall('wyx.*wyx',x) # 贪婪算法(.*),返回wyx与wyx之间的所有内容
print(y)
y = re.findall('wyx.*?wyx',x) # 非贪婪算法(.*?),尽可能少的返回满足正则表达式的结果
print(y)
y = re.findall('wyx(.*?)wyx',x) # 将想提取的内容抠下来
print(y)
output:
['wyxl', 'wyx3', 'wyxy', 'wyx1', 'wyxx', 'wyx3']
['wyxliu', 'wyx3e4', 'wyxyws', 'wyx1q2', 'wyxxia', 'wyx3e4']
['wyx', 'wyx', 'wyx', 'wyx', 'wyxx', 'wyx']
['wyx', 'wyx', 'wyx', 'wyx', 'wyx', 'wyx']
['wyxliuwyx3e4rwyxywshunwyx1q2wwyxxiangwyx']
['wyxliuwyx', 'wyxywshunwyx', 'wyxxiangwyx']
['liu', 'ywshun', 'xiang']
先把网页源代码copy到pic.txt
import os
import re
import requests
from lxml import etree
with open('pic.txt','r',encoding='utf-8') as f:
comtent = f.read()
# print(comtent)
pic_url = re.findall('<img data-v-679affd4="" src="(.*?)" alt',comtent)
headers = {
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.116 Safari/537.36'
}
path = os.path.join(os.getcwd(),'img')
if not os.path.exists(path):
os.mkdir(path)
else:
pass
n = 0
for i in pic_url:
url = i
picture = requests.get(url,headers=headers)
with open(os.path.join(path,str(n)) + '.jpg' , 'wb') as fp:
fp.write(picture.content)
n += 1
print(url,n)