Abdeljalil Arab نشر 8 مايو أرسل تقرير نشر 8 مايو import requests from bs4 import BeautifulSoup import re def bring_data (): url = 'https://coinmarketcap.com/' header = {'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36'} web_request = requests.get(url,headers=header) web_soup = BeautifulSoup(web_request.content, 'html.parser') table_crypto_soup = web_soup.find('table', class_='sc-db1da501-3 ccGPRR cmc-table') re_crypto_names = r'<p class="sc-65e7f566-0 iPbTJf coin-item-name">([\w\s]+)<\/p>' crypto_names = re.findall(re_crypto_names, str(re_crypto_names)) print(crypto_names) bring_data() نتيجة تخرج لي فقط [] اين مشكل 2 اقتباس
0 محمد_عاطف نشر 8 مايو أرسل تقرير نشر 8 مايو المشكلة لديك هنا : بتاريخ 7 دقائق مضت قال Abdeljalil Arab: crypto_names = re.findall(re_crypto_names, str(re_crypto_names)) لاحظ أنك تمرر re_crypto_names في المعاملين . حيث المعامل الثاني هو النص الذي تريد البحث عنه وهذا خاطئ حيث يجب تمرير نص الصفحة وليس التعبير النمطي نفسه . لهذا الصحيح هو إستخدام str(web_soup) هكذا : crypto_names = re.findall(re_crypto_names, str(web_soup)) وهذا هو الكود كاملا : import requests from bs4 import BeautifulSoup import re def bring_data (): url = 'https://coinmarketcap.com/' header = {'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36'} web_request = requests.get(url,headers=header) web_soup = BeautifulSoup(web_request.content, 'html.parser') table_crypto_soup = web_soup.find('table', class_='sc-db1da501-3 ccGPRR cmc-table') re_crypto_names = r'<p class="sc-65e7f566-0 iPbTJf coin-item-name">([\w\s]+)<\/p>' crypto_names = re.findall(re_crypto_names, str(web_soup)) print(crypto_names) bring_data() 1 اقتباس
0 Mustafa Suleiman نشر 8 مايو أرسل تقرير نشر 8 مايو بسبب أنك تبحث عن النمط في النمط نفسه str(re_crypto_names) بدلاً من البحث في محتوى الصفحة table_crypto_soup، هنا: crypto_names = re.findall(re_crypto_names, str(re_crypto_names)) الصحيح هو: crypto_names = re.findall(re_crypto_names, str(table_crypto_soup)) اقتباس
0 بلال زيادة نشر 9 مايو أرسل تقرير نشر 9 مايو المشكلة في الكود تكمن في استخدام re.findall بشكل غير صحيح. أنت تمرر النص الخاص بالتعبير النمطي (re_crypto_names) كمدخل لـ re.findall بدلاً من تمرير محتوى HTML الخاص بالجدول (table_crypto_soup). هذا يؤدي إلى أن re.findall يبحث في النص الخام للتعبير النمطي نفسه، وليس في محتوى الصفحة، وبالتالي يُرجع قائمة فارغة []. crypto_names = re.findall(re_crypto_names, str(re_crypto_names)) re_crypto_names هو النص الذي يمثل التعبير النمطي (regex pattern). str(re_crypto_names) يحول التعبير النمطي إلى سلسلة نصية، وهذا ليس ما تريد البحث فيه. يجب أن تبحث في محتوى الجدول (table_crypto_soup). وتصحيح كامل الكود import requests from bs4 import BeautifulSoup import re def bring_data(): url = 'https://coinmarketcap.com/' header = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36'} web_request = requests.get(url, headers=header) web_soup = BeautifulSoup(web_request.content, 'html.parser') table_crypto_soup = web_soup.find('table', class_='sc-db1da501-3 ccGPRR cmc-table') # التأكد من أن الجدول تم العثور عليه if table_crypto_soup: # البحث في محتوى الجدول باستخدام التعبير النمطي re_crypto_names = r'<p class="sc-65e7f566-0 iPbTJf coin-item-name">([\w\s]+)</p>' crypto_names = re.findall(re_crypto_names, str(table_crypto_soup)) print(crypto_names) else: print("لم يتم العثور على الجدول") bring_data() اقتباس
السؤال
Abdeljalil Arab
نتيجة تخرج لي فقط []
اين مشكل
3 أجوبة على هذا السؤال
Recommended Posts
انضم إلى النقاش
يمكنك أن تنشر الآن وتسجل لاحقًا. إذا كان لديك حساب، فسجل الدخول الآن لتنشر باسم حسابك.