Obtain Links and Download Images from Webpages
作者:XD / 发表: 2023年1月11日 06:13 / 更新: 2023年1月11日 06:13 / 编程笔记 / 阅读量:1007
Obtain Links and Download Images from Webpages
import requests
from bs4 import BeautifulSoup
def getHTMLText(url):
try:
res = requests.get(url, timeout = 6)
res.raise_for_status()
res.encoding = res.apparent_encoding
return res.text
except:
return 'Error'
def main(url):
demo = getHTMLText(url)
soup = BeautifulSoup(demo, 'html.parser')
a_labels = soup.find_all('a', attrs={'href': True})
for idx, a in enumerate(a_labels):
link = a.get('href')
if "res" not in link and ".jpg" in link and idx % 50 == 1:
urls = url + link
save_path = "./save/" + link
with open(save_path, 'wb') as f:
f.write(requests.get(urls).content)
url = "http://eadst.com/"
main(url)
相关标签