先分析一下网页结构:

国家地理爬虫

可以看到图片都是放在div class=‘imgbox’

            div class=‘img’

用requests解析网页,用Beautiful soup将class= ‘img’中的图片链接解析出来,

然后(比较重要的一步):

再次运用requests将图片的链接解析,然后保存。

文件的的创建,按照链接的名字创建链接,具体代码如下所示,具体讲解无。

 国家地理爬虫

 

纯文本:

#国家地理中一篇文档中图片的爬虫
import os
import requests
from bs4 import BeautifulSoup
def craete_dir(name):
if not os.path.exists(name):
os.makedirs(name)
def getlink(url):
herader = {'User-Agent':'Mozilla/5.0'}
req = requests.get(url,headers=herader)
req.encoding=req.apparent_encoding
soup = BeautifulSoup(req.text,'lxml')
soups= soup.findAll('div',class_='imgbox')
for link in soups:
links =link.find('div',class_='img').find('img')
links_a=links.get('src')
#craete_dir('c:/Users/****/Desktop/haha/{}'.format(links_a.split('/')[-1]))
print(links_a)
jieguo = requests.get(links_a)
print(links_a.split('/')[-1][:-5])
craete_dir('c:/Users/***/Desktop/haha')
with open('c:/Users/***/Desktop/haha/{}'.format(links_a.split('/')[-1])[:-5],'wb')as f:
f.write(jieguo.content)
getlink('http://www.dili360.com/article/p5b57eab97878985.htm')

 国家地理爬虫

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-24
  • 2021-06-16
  • 2022-01-08
  • 2021-12-05
  • 2021-12-21
猜你喜欢
  • 2022-12-23
  • 2021-05-23
  • 2022-12-23
  • 2021-08-08
  • 2021-12-02
  • 2021-12-05
  • 2022-12-23
相关资源
相似解决方案