diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5e798fa --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +images \ No newline at end of file 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