|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 一个账号 于 2020-3-14 18:32 编辑
如题,已经获取到每张图片的链接与标题,怎么保存到本地文件夹,我的是错的
- import urllib.request
- import re
- import os
- from bs4 import BeautifulSoup
- def request_url():
- url='https://www.tooopen.com/topiclist/9620.aspx'
- response =urllib.request.Request(url)
- html = urllib.request.urlopen(response)
- #html.add_header('User-Agent','Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36')
- html = html.read().decode('utf-8')
- reg =r'<a class="pic" href="(.*?)" title="(.*?)" target="_blank">'
- photo_url= re.findall(reg,html)
- #print(photo_url[0][0])
- for i in photo_url:
- #urls =i[0]
- #name =i[1]
- urls,name =i
- r =urllib.request.urlopen(urls).read().decode('utf-8')
- #print(r)
- soup =BeautifulSoup(r,"html.parser")
- img_url = soup.find("div",class_="img-list").find_all("a")
-
- reg=r'http(.*?)jpg'
- link = re.findall(reg,str(img_url))
-
- for i in link:
- i='http'+i+'jpg'
- print(i)
- download_imgs = urllib.request.urlopen(i).read()
-
- with open('.jpg','wb') as f:
- f.write(i)
-
- request_url()
-
复制代码
- import urllib.request
- import re
- import os
- import random
- from bs4 import BeautifulSoup
- def request_url():
- url='https://www.tooopen.com/topiclist/9620.aspx'
- response =urllib.request.Request(url)
- html = urllib.request.urlopen(response)
- #html.add_header('User-Agent','Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36')
- html = html.read().decode('utf-8')
- reg =r'<a class="pic" href="(.*?)" title="(.*?)" target="_blank">'
- photo_url= re.findall(reg,html)
- #print(photo_url[0][0])
- for i in photo_url:
- #urls =i[0]
- #name =i[1]
- urls,name =i
- r =urllib.request.urlopen(urls).read().decode('utf-8')
- #print(r)
- soup =BeautifulSoup(r,"html.parser")
- img_url = soup.find("div",class_="img-list").find_all("a")
-
- reg=r'http(.*?)jpg'
- link = re.findall(reg,str(img_url))
-
- for i in link:
- i='http'+i+'jpg'
- print(i)
- download_imgs = urllib.request.urlopen(i).read()
- res = urllib.request.urlopen(i)
- data = res.read()
-
- with open(f'{random.randint(1, 1000)}.jpg','wb') as f:
- f.write(data)
-
- request_url()
复制代码
|
|