
تم تصنيع هذا المشروع من أجل تجريف الويب التلقائي لجعل الكشط سهلاً. يحصل على عنوان URL أو محتوى HTML لصفحة ويب وقائمة من بيانات العينة التي نريد كشط من تلك الصفحة. يمكن أن تكون هذه البيانات نصية أو عنوان URL أو أي قيمة علامة HTML لتلك الصفحة. إنه يتعلم قواعد الكشط ويعيد العناصر المماثلة. بعد ذلك ، يمكنك استخدام هذا الكائن المستفاد مع عناوين URL جديدة للحصول على محتوى مماثل أو نفس العنصر الدقيق لتلك الصفحات الجديدة.
إنه متوافق مع بيثون 3.
$ pip install git+https://github.com/alirezamika/autoscraper.git$ pip install autoscraper$ python setup.py installلنفترض أننا نريد جلب جميع عناوين النشر ذات الصلة في صفحة Stackoverflow:
from autoscraper import AutoScraper
url = 'https://stackoverflow.com/questions/2081586/web-scraping-with-python'
# We can add one or multiple candidates here.
# You can also put urls here to retrieve urls.
wanted_list = [ "What are metaclasses in Python?" ]
scraper = AutoScraper ()
result = scraper . build ( url , wanted_list )
print ( result )ها هو الإخراج:
[
'How do I merge two dictionaries in a single expression in Python (taking union of dictionaries)?' ,
'How to call an external command?' ,
'What are metaclasses in Python?' ,
'Does Python have a ternary conditional operator?' ,
'How do you remove duplicates from a list whilst preserving order?' ,
'Convert bytes to a string' ,
'How to get line count of a large file cheaply in Python?' ,
"Does Python have a string 'contains' substring method?" ,
'Why is “1000000000000000 in range(1000000000000001)” so fast in Python 3?'
] يمكنك الآن استخدام كائن scraper للحصول على مواضيع ذات صلة من أي صفحة Stackoverflow:
scraper . get_result_similar ( 'https://stackoverflow.com/questions/606191/convert-bytes-to-a-string' )لنفترض أننا نريد كشط أسعار الأسهم المباشرة من ياهو تمويل:
from autoscraper import AutoScraper
url = 'https://finance.yahoo.com/quote/AAPL/'
wanted_list = [ "124.81" ]
scraper = AutoScraper ()
# Here we can also pass html content via the html parameter instead of the url (html=html_content)
result = scraper . build ( url , wanted_list )
print ( result ) لاحظ أنه يجب عليك تحديث wanted_list إذا كنت ترغب في نسخ هذا الرمز ، حيث يتغير محتوى الصفحة ديناميكيًا.
يمكنك أيضًا تمرير أي معلمة وحدة requests مخصصة. على سبيل المثال ، قد ترغب في استخدام الوكلاء أو الرؤوس المخصصة:
proxies = {
"http" : 'http://127.0.0.1:8001' ,
"https" : 'https://127.0.0.1:8001' ,
}
result = scraper . build ( url , wanted_list , request_args = dict ( proxies = proxies ))الآن يمكننا الحصول على سعر أي رمز:
scraper . get_result_exact ( 'https://finance.yahoo.com/quote/MSFT/' ) قد ترغب في الحصول على معلومات أخرى كذلك. على سبيل المثال ، إذا كنت ترغب في الحصول على الحد الأقصى للسوق أيضًا ، فيمكنك فقط إلحاقها بالقائمة المطلوبة. باستخدام طريقة get_result_exact ، سيقوم باسترداد بيانات نفس الترتيب الدقيق في القائمة المطلوبة.
مثال آخر: قل أننا نريد كشط النص حول النص وعدد النجوم ورابط إلى قضايا صفحات جيثب ريبو:
from autoscraper import AutoScraper
url = 'https://github.com/alirezamika/autoscraper'
wanted_list = [ 'A Smart, Automatic, Fast and Lightweight Web Scraper for Python' , '6.2k' , 'https://github.com/alirezamika/autoscraper/issues' ]
scraper = AutoScraper ()
scraper . build ( url , wanted_list )بسيط ، أليس كذلك؟
يمكننا الآن حفظ النموذج المبني لاستخدامه لاحقًا. لإنقاذ:
# Give it a file path
scraper . save ( 'yahoo-finance' )ولتحميل:
scraper . load ( 'yahoo-finance' )لا تتردد في فتح مشكلة إذا كان لديك أي مشكلة باستخدام الوحدة النمطية.