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

تحميل ملفات PDF باستخدام BeautifulSoup في بايثون

إياد أحمد

السؤال

Recommended Posts

  • 1

لتحميل ملف PDF، يمكنك اتباع الخطوات التالية:
1. استيراد مكتبتي BeautifulSoup  و requests.
2. طلب عنوان URL والحصول على الاستجابة (من خلال مكتبة requests يمكننا تقديم الطلب والحصول على كائن استجابة).
3. البحث عن جميع الروابط الموجودة على صفحة الويب.
4. التحقق من وجود روابط PDF في تلك الروابط.
5. الحصول على ملف ال PDF من خلال كائن الاستجابة.

# استيراد الوحدات
from bs4 import BeautifulSoup
import requests
# تحديد رابط الموقع الهدف 
url = "page_link"
# والحصول على كائن الاستجابة url طلب عنوان 
responseObj = requests.get(url)
# BeautifulSoup تحليل النص الذي تم الحصول عليه من خلال 
soup = BeautifulSoup(responseObj.text, 'lxml')
# إيجاد كل الروابط
allLinks = soup.find_all('a')
pdfNum = 0
# نقوم بتحميله pdf الآن نقوم بالمرور على كل الروابط، وعند العثور على ملف 
for link in allLinks:
	if ('.pdf' in link.get('href', [])):
		pdfNum += 1
		print("Downloading file number: ", pdfNum)
		# الحصول على كائن استجابة للرابط
		responseObj = requests.get(link.get('href'))
		# pdf كتابة المحتوى في ملف 
		pdf = open("pdf"+str(pdfNum)+".pdf", 'wb')
		pdf.write(responseObj.content)
		pdf.close()
		print("File ", pdfNum, " downloaded")
print("All PDF files downloaded")

 

رابط هذا التعليق
شارك على الشبكات الإجتماعية

  • 0

يمكنك كذلك استخدام طريقة مبسطة لتحميل ملفات ال pdf مباشرة كالتالي:

#استدعاء المكتبات
import requests
from bs4 import BeautifulSoup as bs
import urllib2

_URL = "url link"

r = requests.get(_URL)
soup = bs(r.text)
urls = []
names = []
for i, link in enumerate(soup.findAll('a')):
    _FULLURL = _URL + link.get('href')
    if _FULLURL.endswith('.pdf'):
        urls.append(_FULLURL)
        names.append(soup.select('a')[i].attrs['href'])

names_urls = zip(names, urls)

for name, url in names_urls:
    print url
    rq = urllib2.Request(url)
    res = urllib2.urlopen(rq)
    pdf = open("pdfs/" + name, 'wb')
    pdf.write(res.read())
    pdf.close()

وكذلك يمكنك تحميل كافة ملفات ال pdf داخل صفحة بسهولة كالتالي:

#استدعاء المكتبات
import os
import requests
from urllib.parse import urljoin
from bs4 import BeautifulSoup

url = "url link"

#عمل ملف لحفظ ما سيتم تحميله فيه
folder_location = r'E:\webscraping'
#خلق الملف اذا لميكن يوجد
if not os.path.exists(folder_location):os.mkdir(folder_location)

response = requests.get(url)
soup= BeautifulSoup(response.text, "html.parser")     
for link in soup.select("a[href$='.pdf']"):
    #تسمية الملفات المحملة
    filename = os.path.join(folder_location,link['href'].split('/')[-1])
    with open(filename, 'wb') as f:
        f.write(requests.get(urljoin(url,link['href'])).content)

 

تم التعديل في بواسطة Ahmed Sharshar
رابط هذا التعليق
شارك على الشبكات الإجتماعية

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

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

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

×   لقد أضفت محتوى بخط أو تنسيق مختلف.   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.

  • إعلانات

  • تابعنا على



×
×
  • أضف...