首页 > 编程语言 > 详细

[原创]python爬虫之BeautifulSoup,爬取网页上所有图片标题并存储到本地文件

时间:2019-05-15 15:36:40      阅读:656      评论:0      收藏:0      [点我收藏+]
from bs4 import BeautifulSoup
import requests
import re
import os
r = requests.get("https://re.jd.com/search?keyword=%E6%B0%B4%E6%9E%9C%20%E7%BD%91&keywordid=44195495794&re_dcp=202m0QjIIg==&traffic_source=1004&test=1&enc=utf8&cu=true&utm_source=baidu-search&utm_medium=cpc&utm_campaign=t_262767352_baidusearch&utm_term=44195495794_0_32d58cbc7f0f40e08d64a09fbc8c95c4")
result = r.content
# print(result)
soup = BeautifulSoup(result,"html.parser")
# print(soup.script.text)
souptext = soup.find(type=text/javascript).text
# print(souptext)

pattern3 =re.compile(r\"ad_title_text\":\"(.*?\"),\"image_url\":\"(.*?\.(jpg|png))\")
patternresult3 = pattern3.findall(souptext)
print(patternresult3)

j = 0
for i in patternresult3:
    j = j+1
    title = i[0].replace( ,‘‘).replace(\",1).replace(/,1)
    with open(os.getcwd()+\\jpg\\+title+str(j)+"."+i[2],"wb") as f: #在执行代码前,需要先创建一个jpg的目录,os.getcwd()用来获取当前目录
        f.write(requests.get("https://img1.360buyimg.com/n6/"+i[1]).content)

运行结果如下:

技术分享图片

 

[原创]python爬虫之BeautifulSoup,爬取网页上所有图片标题并存储到本地文件

原文:https://www.cnblogs.com/lelexiong/p/10869451.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!