关于爬虫
写出的爬虫爬到的图片是这样子的
怎么办import requests
import re
import time
def get_html(url):
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36'
}
html=requests.get(url,headers=headers)
return html
def get_img_list(html):
html=html.text
a = re.compile(r"<img class='lazy' src='.*?data-original='(.*?[.]jpg)'", re.S)
img_list=re.findall(a,html)
return img_list
def main():
print('欢迎使用爬虫')
n=int(input('请输入需要保存保存图片的网页数:'))
m=0
for i in range(1,n+1):
url = 'https://www.mzitu.com/japan/page/'+str(i)+'/'
page_html = get_html(url)
img_list = get_img_list(page_html)
print(img_list)
n=1+m
for each_img_link in img_list:
file_name='美眉/'+ str(n)+'.jpg'
with open(file_name, 'wb') as f:
img_html=get_html(each_img_link)
print('开始保存')
f.write(img_html.content)
print('爬取成功')
n+=1
time.sleep(2)
m+=1000
if __name__ == '__main__':
main() 本帖最后由 Twilight6 于 2020-7-11 08:03 编辑
headers 里面加上 'Referer': 'https://www.mzitu.com/'
import requests
import re
import time
def get_html(url):
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36',
'Referer': 'https://www.mzitu.com/'
}
html=requests.get(url,headers=headers)
return html
def get_img_list(html):
html=html.text
a = re.compile(r"<img class='lazy' src='.*?data-original='(.*?[.]jpg)'", re.S)
img_list=re.findall(a,html)
return img_list
def main():
print('欢迎使用爬虫')
n=int(input('请输入需要保存保存图片的网页数:'))
m=0
for i in range(1,n+1):
url = 'https://www.mzitu.com/japan/page/'+str(i)+'/'
page_html = get_html(url)
img_list = get_img_list(page_html)
print(img_list)
n=1+m
for each_img_link in img_list:
file_name='美眉'+ str(n)+'.jpg'
with open(file_name, 'wb') as f:
img_html=get_html(each_img_link)
print('开始保存')
f.write(img_html.content)
print('爬取成功')
n+=1
time.sleep(2)
m+=1000
if __name__ == '__main__':
main() 看一楼{:10_275:} Twilight6 发表于 2020-7-11 08:01
headers 里面加上 'Referer': 'https://www.mzitu.com/'
Referer的值就是填上一个页面的网址吗
页:
[1]