اذهب إلى المحتوى

السؤال

نشر
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()

نتيجة تخرج لي فقط [] 
اين مشكل

Recommended Posts

  • 0
نشر

المشكلة لديك هنا :

بتاريخ 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()

 

  • 0
نشر

المشكلة في الكود تكمن في استخدام 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()

 

انضم إلى النقاش

يمكنك أن تنشر الآن وتسجل لاحقًا. إذا كان لديك حساب، فسجل الدخول الآن لتنشر باسم حسابك.

زائر
أجب على هذا السؤال...

×   لقد أضفت محتوى بخط أو تنسيق مختلف.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   جرى استعادة المحتوى السابق..   امسح المحرر

×   You cannot paste images directly. Upload or insert images from URL.

  • إعلانات

  • تابعنا على



×
×
  • أضف...