数据在网页源代码中用正则
案例一(饭前点心)
import urllib.request
url = 'https://tieba.baidu.com/p/5734214281'
response=urllib.request.urlopen(url)
html=response.read()
html=html.decode('utf-8')
html
运行截图如下:
import re
reg=r'src="(.+?\.jpg)" pic_ext' #r在这里是来识别双引号的,避免与python中字符串的双引号用法冲突
imgre=re.compile(reg)#编译正则表达式
imglist=re.findall(imgre,html)
x=1
for i in imglist:
print(x)
urllib.request.urlretrieve(i,'C:\\Users\\18487\\Desktop\\picture\\%s.jpg'%x)
x+=1
print("done")
案例二:爬取任何你想要的有关图片(大餐来啦)
import requests
import re
import os
import time
x = 1 #图片计数
def getHtml(url):
#proxies={"http":"http://1.197.16.148:9999"} #代理IP,反反爬
#headers={'User-Agent':'Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1',
#'Cookie': '_tact=MWJkMmI3MzEtOTYx' } #iPhone的header并带有cookie
#html=requests.get(url,headers=headers,proxies=proxies).text
headers={
'Accept-Encoding':'gzip,deflate',
'Accept-Language':'zh-CN',
'Cache-Control':'no-cache',
'Connection':'keep-alive',
'Pragma':'no-cache',
'User-Agent':'Mozilla/5.0