您好,我正在嘗試制作一個刮板,將網頁中的所有 .css 檔案保存在檔案夾中,但是在運行我的腳本時出現此錯誤:
with open(shit, 'wb') as f: FileNotFoundError: [Errno 2] No such file or directory: 'https://url.com/cache/themes/theme1/index.min.css'
這是我的代碼:
from bs4 import BeautifulSoup
import requests
import os
proxies = {
'http': 'socks5h://127.0.0.1:9050',
'https': 'socks5h://127.0.0.1:9050'
}
url = "https://url.com"
folder = "Files"
resp = requests.get(url, proxies=proxies)
soup = BeautifulSoup(resp.text, features='lxml')
def Downloader(url, folder):
os.mkdir(os.path.join(os.getcwd(), folder))
os.chdir(os.path.join(os.getcwd(), folder))
css = soup.find_all('link', rel="stylesheet")
for cum in css:
shit = cum['href']
if "http://" in shit:
with open(shit, 'wb') as f:
piss = requests.get(shit, proxies=proxies)
f.write(piss.content)
Downloader(url=url, folder=folder)
有誰知道問題可能是什么?謝謝<3
uj5u.com熱心網友回復:
您正在嘗試使用其中包含的名稱寫入檔案/
。這將其標識為目錄/檔案夾的一部分。因此,要么洗掉/替換那些,要么構建創建這些檔案夾結構的邏輯,它會寫入檔案。
import requests
from bs4 import BeautifulSoup
import os
proxies = {
'http': 'socks5h://127.0.0.1:9050',
'https': 'socks5h://127.0.0.1:9050'
}
url = "https://url.com"
folder = "Files"
resp = requests.get(url, proxies=proxies)
soup = BeautifulSoup(resp.text, features='lxml')
def Downloader(url, folder):
try:
os.mkdir(os.path.join(os.getcwd(), folder))
except Exception as e:
print(e)
os.chdir(os.path.join(os.getcwd(), folder))
css = soup.find_all('link', rel="stylesheet")
for each in css:
href = each['href']
if "http://" in href or "https://" in href:
filename = href.split('//')[-1].replace('/','_').replace('.','_')
with open(filename, 'wb') as f:
response = requests.get(href, proxies=proxies)
f.write(response.content)
Downloader(url=url, folder=folder)
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/468766.html
標籤:Python python-3.x 网页抓取 美丽的汤