diff --git a/.gitignore b/.gitignore index 6769e21..bd48140 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ +images + # Byte-compiled / optimized / DLL files __pycache__/ *.py[cod] @@ -157,4 +159,4 @@ cython_debug/ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore # and can be added to the global gitignore or merged into this file. For a more nuclear # option (not recommended) you can uncomment the following to ignore the entire idea folder. -#.idea/ \ No newline at end of file +#.idea/ diff --git a/scrape_data.py b/scrape_data.py index 6214254..af05b51 100644 --- a/scrape_data.py +++ b/scrape_data.py @@ -1,7 +1,7 @@ import requests from bs4 import BeautifulSoup from lxml import etree -import re +import os ROOT_URL = "https://myanimelist.net/character.php" @@ -12,16 +12,24 @@ def get_page_xpath_result(url, xpath_str): dom = etree.HTML(str(soup)) return dom.xpath(xpath_str) -# 1. face image -# 2. character name -# 3. link - character_links = get_page_xpath_result(ROOT_URL, '//div[@class="information di-ib mt24"]/a/@href') character_names = get_page_xpath_result(ROOT_URL, '//div[@class="information di-ib mt24"]/a') character_names = [link.text for link in character_names] -print("character_links") -print(character_links) +character_img_urls = [get_page_xpath_result(link, '//td/div/a/img/@data-src')[0] + for link in character_links] + +path = './images' +isExist = os.path.exists(path) +if not isExist: + os.makedirs(path) + print("The images directory is created!") + +for name, url in zip(character_names, character_img_urls): + img_data = requests.get(url).content + with open(f'images/{name}.jpg', 'wb') as handler: + handler.write(img_data) + handler.close() + print(f'{name}.jpg downloaded') + -print("character_names") -print(character_names) \ No newline at end of file