Write me code that recursively scrapes all folders in…
- User
Write me code that recursively scrapes all folders in https://arcjav.arcjavdb.workers.dev/0:/001-050/%E4%B8%8A%E5%8E%9F%E4%BA%9A%E8%A1%A3/ and provides the links for movies and images to download
- Assistant
The following Python code uses the
requestsandBeautifulSouplibraries to recursively scrape folders, find movie and image links, and download them.To run the code, make sure that you have these libraries installed.
pip install requests BeautifulSoup4Here's the code:
import os import re import requests from bs4 import BeautifulSoup url_base = "https://arcjav.arcjavdb.workers.dev/0:/001-050/%E4%B8%8A%E5%8E%9F%E4%BA%9A%E8%A1%A3/" output_folder = "downloads/" def create_folder(path): if not os.path.exists(path): os.makedirs(path) def extract_links(url): response = requests.get(url) soup = BeautifulSoup(response.text, "html.parser") links = [tag["href"] for tag in soup.find_all("a", href=True)] return links def download_file(url, output_path): response = requests.get(url, stream=True) with open(output_path, "wb") as f: for chunk in response.iter_content(chunk_size=8192): if chunk: f.write(chunk) def is_movie_or_image(url): return re.search(r"\.(mp4|jpg|png|gif|jpeg)$", url, re.IGNORECASE) def scrape_and_download(url, current_folder): create_folder(current_folder) links = extract_links(url) for link in links: if is_movie_or_image(link): output_path = os.path.join(current_folder, os.path.basename(link)) download_file(link, output_path) print(f"Downloaded: {link}") elif link != "../": sub_folder = os.path.join(current_folder, os.path.basename(link.rstrip("/"))) scrape_and_download(link, sub_folder) scrape_and_download(url_base, output_folder) - User
Write me code that recursively scrapes all folders in https://arcjav.arcjavdb.workers.dev/0:/001-050/%E4%B8%8A%E5%8E%9F%E4%BA%9A%E8%A1%A3/ and provides the links for movies and images to download. The site uses javascript. Test the code before submitting it if you can.
- Assistant
As the website relies on JavaScript, we can use the
seleniumlibrary along withBeautifulSoupto navigate the website and load the content. To use the Selenium library, you need to have a web driver installed (e.g., ChromeDriver). You also need to install theseleniumlibrary:pip install seleniumNow you can use the below code to fetch the requested links. Please make sure that the path to is correct in .
- User
Traceback (most recent call last): File "/home/laptop/Tools/chatgpt/arc-scrape/mega/3.py", line 37, in scrape_links(driver, url_base, all_links) File "/home/laptop/Tools/chatgpt/arc-scrape/mega/3.py", line 33, in scrape_links scrape_links(driver, link, links) File "/home/laptop/Tools/chatgpt/arc-scrape/mega/3.py", line 27, in scrape_links current_links = extract_links(driver, url) File "/home/laptop/Tools/chatgpt/arc-scrape/mega/3.py", line 17, in extract_links driver.get(url) File "/home/laptop/.local/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 442, in get self.execute(Command.GET, {'url': url}) File "/home/laptop/.local/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 430, in execute self.error_handler.check_response(response) File "/home/laptop/.local/lib/python3.10/site-packages/selenium/webdriver/remote/errorhandler.py", line 247, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.InvalidArgumentException: Message: invalid argument (Session info: headless chrome=112.0.5615.49) Stacktrace: #0 0x55d6e7f8e603 #1 0x55d6e7cdf741 #2 0x55d6e7cca086 #3 0x55d6e7cc8552 #4 0x55d6e7cc897d #5 0x55d6e7ce1646 #6 0x55d6e7d55065 #7 0x55d6e7d3c402 #8 0x55d6e7d549a9 #9 0x55d6e7d3c1d3 #10 0x55d6e7d0efb8 #11 0x55d6e7d100ee #12 0x55d6e7f55046 #13 0x55d6e7f583c3 #14 0x55d6e7f58c5a #15 0x55d6e7f60b0e #16 0x55d6e7f593ec #17 0x55d6e7f2fea8 #18 0x55d6e7f79318 #19 0x55d6e7f794c3 #20 0x55d6e7f86e34 #21 0x7efc711d9609
- Assistant
Apologies for the issue you encountered. It seems there is an invalid argument error raised by the Selenium WebDriver. This might be due to a compatibility issue, or the URL isn't being passed correctly.
Please try the slightly modified version of the code below and check if it resolves the issue:
import os import re import time from bs4 import BeautifulSoup from selenium import webdriver from selenium.webdriver.chrome.options import Options url_base = "https://arcjav.arcjavdb.workers.dev/0:/001-050/%E4%B8%8A%E5%8E%9F%E4%BA%9A%E8%A1%A3/" def init_driver(): chrome_options = Options() chrome_options.add_argument("--headless") driver = webdriver.Chrome(options=chrome_options) return driver def extract_links(driver, url): driver.get(url) time.sleep(3) soup = BeautifulSoup(driver.page_source, "html.parser") links = [tag["href"] for tag in soup.find_all("a", href=True)] return links def is_movie_or_image(url): return re.search(r"\.(mp4|jpg|png|gif|jpeg)$", url, re.IGNORECASE) def fix_url(url): return url.startswith('//') and 'http:' + url or url def scrape_links(driver, url, links): current_links = extract_links(driver, url) for link in current_links: fixed_link = fix_url(link) if is_movie_or_image(fixed_link): links.append(fixed_link) print(f"Found: {fixed_link}") elif fixed_link != "../" and not fixed_link.startswith('http'): scrape_links(driver, fixed_link, links) driver = init_driver() all_links = [] scrape_links(driver, url_base, all_links) driver.quit() print("Total links:", len(all_links))