Last active
January 17, 2025 11:28
-
-
Save Xnuvers007/c92bd0ddf0b1543f2e2c9fedf6a22839 to your computer and use it in GitHub Desktop.
scraping upcoming event MLBB at https://mobile-legends.fandom.com/wiki/Upcoming_content
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
from bs4 import BeautifulSoup | |
from flask import Flask, jsonify | |
app = Flask(__name__) | |
@app.route('/mlbb', methods=['GET']) | |
def get_upcoming_skins(): | |
url = "https://mobile-legends.fandom.com/wiki/Upcoming_content" | |
headers = { | |
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3' | |
} | |
response = requests.get(url, headers=headers, timeout=5) | |
response.raise_for_status() | |
soup = BeautifulSoup(response.content, 'html.parser') | |
table = soup.find('table', {'class': 'wikitable'}) | |
rows = table.find_all('tr')[2:] | |
skins = [] | |
for row in rows: | |
cols = row.find_all('td') | |
if cols: | |
hero_image = cols[0].find('img')['src'] if cols[0].find('img') else "No Image" | |
hero_name = cols[1].text.strip() if len(cols) > 1 else "Unknown" | |
skin_name = cols[2].text.strip() if len(cols) > 2 else "N/A" | |
release_date = cols[3].text.strip() if len(cols) > 3 else "TBD" | |
event_type = cols[4].text.strip() if len(cols) > 4 else "TBD" | |
skins.append({ | |
'hero_name': hero_name, | |
'skin_name': skin_name, | |
'release_date': release_date, | |
'event_type': event_type, | |
'hero_image': hero_image | |
}) | |
galleries = [] | |
for gallery_id in range(0, 24): | |
gallery_div = soup.find('div', {'id': f'gallery-{gallery_id}'}) | |
if gallery_div: | |
previous_span = gallery_div.find_previous('span', class_="mw-headline") | |
description = previous_span.text.strip() if previous_span else "No Description" | |
gallery_items = gallery_div.find_all('div', class_='wikia-gallery-item') | |
gallery_items_data = [] | |
for item in gallery_items: | |
hero_caption = item.find('div', class_='lightbox-caption').text.strip() if item.find('div', class_='lightbox-caption') else "No Caption" | |
img_tag = item.find('img') | |
hero_image_url = img_tag['data-src'] if img_tag and 'data-src' in img_tag.attrs else img_tag['src'] if img_tag else "No Image" | |
gallery_items_data.append({ | |
'caption': hero_caption, | |
'image_url': hero_image_url | |
}) | |
galleries.append({ | |
'gallery_id': gallery_id, | |
'description': description, | |
'gallery_items': gallery_items_data | |
}) | |
return jsonify({ | |
'skins': skins, | |
'galleries': galleries | |
}) | |
if __name__ == '__main__': | |
app.run(debug=True) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
from bs4 import BeautifulSoup | |
url = "https://mobile-legends.fandom.com/wiki/Upcoming_content" | |
headers = { | |
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3' | |
} | |
response = requests.get(url, headers=headers, timeout=5) | |
response.raise_for_status() | |
soup = BeautifulSoup(response.content, 'html.parser') | |
table = soup.find('table', {'class': 'wikitable'}) | |
rows = table.find_all('tr')[2:] | |
print(f"{'Hero':<15} {'Skin Name':<25} {'Release Date':<15} {'Type':<20} {'Image':<10}") | |
for row in rows: | |
cols = row.find_all('td') | |
if cols: | |
hero_image = cols[0].find('img')['src'] if cols[0].find('img') else "No Image" | |
hero_name = cols[1].text.strip() if len(cols) > 1 else "Unknown" | |
skin_name = cols[2].text.strip() if len(cols) > 2 else "N/A" | |
release_date = cols[3].text.strip() if len(cols) > 3 else "TBD" | |
event_type = cols[4].text.strip() if len(cols) > 4 else "TBD" | |
print(f"{hero_name:<15} {skin_name:<25} {release_date:<15} {event_type:<20} {hero_image:<10}") | |
else: | |
pass | |
for gallery_id in range(0, 24): | |
gallery_div = soup.find('div', {'id': f'gallery-{gallery_id}'}) | |
if gallery_div: | |
previous_span = gallery_div.find_previous('span', class_="mw-headline") | |
description = previous_span.text.strip() if previous_span else "No Description" | |
gallery = gallery_div | |
print(f"\nGallery {gallery_id}:") | |
gallery_items = gallery.find_all('div', class_='wikia-gallery-item') | |
print(f"Description: {description}") | |
for item in gallery_items: | |
hero_caption = item.find('div', class_='lightbox-caption').text.strip() if item.find('div', class_='lightbox-caption') else "No Caption" | |
img_tag = item.find('img') | |
hero_image_url = img_tag['data-src'] if img_tag and 'data-src' in img_tag.attrs else img_tag['src'] if img_tag else "No Image" | |
print(f"Caption: {hero_caption} | Image URL: {hero_image_url}") | |
else: | |
print(f"Gallery {gallery_id}: No content found.") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
from bs4 import BeautifulSoup | |
url = "https://mobile-legends.fandom.com/wiki/Upcoming_content" | |
headers = { | |
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3' | |
} | |
response = requests.get(url, headers=headers, timeout=5) | |
response.raise_for_status() | |
soup = BeautifulSoup(response.content, 'html.parser') | |
table = soup.find('table', {'class': 'wikitable'}) | |
if table: | |
rows = table.find_all('tr')[2:] | |
print(f"{'Hero':<15} {'Skin Name':<25} {'Release Date':<15} {'Type':<20} {'Image URL':<10}") | |
for row in rows: | |
cols = row.find_all('td') | |
if len(cols) < 5: | |
continue | |
hero_image_url = cols[0].find('img')['src'] if cols[0].find('img') else "No Image" | |
hero_name = cols[1].text.strip() | |
skin_name = cols[2].text.strip() | |
release_date = cols[3].text.strip() if cols[3].text else "TBD" | |
event_type = cols[4].text.strip() if cols[4].text else "TBD" | |
print(f"{hero_name:<15} {skin_name:<25} {release_date:<15} {event_type:<20} {hero_image_url:<10}") | |
else: | |
print("No upcoming skins table found.") | |
for gallery_id in range(0, 24): | |
gallery_div = soup.find('div', {'id': f'gallery-{gallery_id}'}) | |
if gallery_div: | |
previous_span = gallery_div.find_previous('span', class_="mw-headline") | |
description = previous_span.text.strip() if previous_span else "No Description" | |
gallery = gallery_div | |
print(f"\nGallery {gallery_id}:") | |
gallery_items = gallery.find_all('div', class_='wikia-gallery-item') | |
print(f"Description: {description}") | |
for item in gallery_items: | |
hero_caption = item.find('div', class_='lightbox-caption').text.strip() if item.find('div', class_='lightbox-caption') else "No Caption" | |
img_tag = item.find('img') | |
hero_image_url = img_tag['data-src'] if img_tag and 'data-src' in img_tag.attrs else img_tag['src'] if img_tag else "No Image" | |
print(f"Caption: {hero_caption} | Image URL: {hero_image_url}") | |
else: | |
print(f"Gallery {gallery_id}: No content found.") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask, jsonify | |
import requests | |
from bs4 import BeautifulSoup | |
app = Flask(__name__) | |
@app.route('/mlbb', methods=['GET']) | |
def get_upcoming_content(): | |
url = "https://mobile-legends.fandom.com/wiki/Upcoming_content" | |
headers = { | |
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3' | |
} | |
response = requests.get(url, headers=headers, timeout=5) | |
response.raise_for_status() | |
soup = BeautifulSoup(response.content, 'html.parser') | |
data = {} | |
# Get upcoming skins data | |
table = soup.find('table', {'class': 'wikitable'}) | |
if table: | |
rows = table.find_all('tr')[2:] | |
upcoming_skins = [] | |
for row in rows: | |
cols = row.find_all('td') | |
if len(cols) < 5: | |
continue | |
hero_image_url = cols[0].find('img')['src'] if cols[0].find('img') else "No Image" | |
hero_name = cols[1].text.strip() | |
skin_name = cols[2].text.strip() | |
release_date = cols[3].text.strip() if cols[3].text else "TBD" | |
event_type = cols[4].text.strip() if cols[4].text else "TBD" | |
upcoming_skins.append({ | |
'hero_name': hero_name, | |
'skin_name': skin_name, | |
'release_date': release_date, | |
'event_type': event_type, | |
'hero_image_url': hero_image_url | |
}) | |
data['upcoming_skins'] = upcoming_skins | |
else: | |
data['upcoming_skins'] = "No upcoming skins table found." | |
# Get gallery data | |
galleries = [] | |
for gallery_id in range(0, 24): | |
gallery_div = soup.find('div', {'id': f'gallery-{gallery_id}'}) | |
if gallery_div: | |
previous_span = gallery_div.find_previous('span', class_="mw-headline") | |
description = previous_span.text.strip() if previous_span else "No Description" | |
gallery_items = gallery_div.find_all('div', class_='wikia-gallery-item') | |
gallery_data = { | |
'gallery_id': gallery_id, | |
'description': description, | |
'items': [] | |
} | |
for item in gallery_items: | |
hero_caption = item.find('div', class_='lightbox-caption').text.strip() if item.find('div', class_='lightbox-caption') else "No Caption" | |
img_tag = item.find('img') | |
hero_image_url = img_tag['data-src'] if img_tag and 'data-src' in img_tag.attrs else img_tag['src'] if img_tag else "No Image" | |
gallery_data['items'].append({ | |
'caption': hero_caption, | |
'image_url': hero_image_url | |
}) | |
galleries.append(gallery_data) | |
else: | |
galleries.append({ | |
'gallery_id': gallery_id, | |
'description': "No content found.", | |
'items': [] | |
}) | |
data['galleries'] = galleries | |
return jsonify(data) | |
if __name__ == '__main__': | |
app.run(debug=False) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment