Mysql
 sql >> डेटाबेस >  >> RDS >> Mysql

Mysql में सुंदर सूप वेबस्क्रैप

इसलिए यहां कुछ बातों का ध्यान रखना है।

PyMySQL पर डॉक्स आपको जगाने और दौड़ने में बहुत अच्छे हैं।

इससे पहले कि आप इन चीजों को डेटाबेस में डाल सकें, आपको उन्हें इस तरह से पकड़ना होगा कि कलाकार और गीत का नाम एक-दूसरे से जुड़ा हो। अभी आपको कलाकारों और गानों की एक अलग सूची मिल रही है, उन्हें जोड़ने का कोई तरीका नहीं है। ऐसा करने के लिए आप शीर्षक-कलाकार वर्ग पर पुनरावृति करना चाहेंगे।

मैं ऐसा ही करूंगा -

from urllib import urlopen
from bs4 import BeautifulSoup
import pymysql.cursors

# Webpage connection
html = urlopen("http://www.officialcharts.com/charts/singles-chart/19800203/7501/")

# Grab title-artist classes and iterate
bsObj = BeautifulSoup(html)
recordList = bsObj.findAll("div", {"class" : "title-artist",})

# Now iterate over recordList to grab title and artist
for record in recordList:
     title = record.find("div", {"class": "title",}).get_text().strip()
     artist = record.find("div", {"class": "artist"}).get_text().strip()
     print artist + ': ' + title

यह रिकॉर्डलिस्ट लूप के प्रत्येक पुनरावृत्ति के लिए शीर्षक और कलाकार को प्रिंट करेगा।

इन मानों को एक MySQL DB में सम्मिलित करने के लिए, मैंने artist_song . नामक एक तालिका बनाई निम्नलिखित के साथ:

CREATE TABLE `artist_song` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `artist` varchar(255) COLLATE utf8_bin NOT NULL,
  `song` varchar(255) COLLATE utf8_bin NOT NULL,
  PRIMARY KEY (`id`)
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin
  AUTO_INCREMENT=1;

इस बारे में जाने का यह सबसे साफ तरीका नहीं है, लेकिन विचार सही है। हम MySQL डीबी से एक कनेक्शन खोलना चाहते हैं (मैंने अपना डीबी टॉप_40 कहा है), और रिकॉर्डलिस्ट लूप के प्रत्येक पुनरावृत्ति के लिए एक कलाकार/शीर्षक जोड़ी डालें:

from urllib import urlopen
from bs4 import BeautifulSoup
import pymysql.cursors


# Webpage connection
html = urlopen("http://www.officialcharts.com/charts/singles-chart/19800203/7501/")

# Grab title-artist classes and store in recordList
bsObj = BeautifulSoup(html)
recordList = bsObj.findAll("div", {"class" : "title-artist",})

# Create a pymysql cursor and iterate over each title-artist record.
# This will create an INSERT statement for each artist/pair, then commit
# the transaction after reaching the end of the list. pymysql does not
# have autocommit enabled by default. After committing it will close
# the database connection.
# Create database connection

connection = pymysql.connect(host='localhost',
                             user='root',
                             password='password',
                             db='top_40',
                             charset='utf8mb4',
                             cursorclass=pymysql.cursors.DictCursor)

try:
    with connection.cursor() as cursor:
        for record in recordList:
            title = record.find("div", {"class": "title",}).get_text().strip()
            artist = record.find("div", {"class": "artist"}).get_text().strip()
            sql = "INSERT INTO `artist_song` (`artist`, `song`) VALUES (%s, %s)"
            cursor.execute(sql, (artist, title))
    connection.commit()
finally:
    connection.close()

संपादित करें:मेरी टिप्पणी के अनुसार, मुझे लगता है कि इसके बजाय तालिका पंक्तियों पर पुनरावृति करना स्पष्ट है:

from urllib import urlopen
from bs4 import BeautifulSoup
import pymysql.cursors


# Webpage connection
html = urlopen("http://www.officialcharts.com/charts/singles-chart/19800203/7501/")

bsObj = BeautifulSoup(html)

rows = bsObj.findAll('tr')
for row in rows:
    if row.find('span', {'class' : 'position'}):
        position = row.find('span', {'class' : 'position'}).get_text().strip()
        artist = row.find('div', {'class' : 'artist'}).get_text().strip()
        track = row.find('div', {'class' : 'title'}).get_text().strip()



  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. MySQLdb आयात नहीं कर सकता - अजगर - विंडोज 8.1

  2. MySQL DDL उत्पन्न करने के लिए Visio का उपयोग करना

  3. mysql लाने सरणी

  4. सम्मिलित तालिका में कुछ शर्तों को पूरा नहीं करने वाले सभी रिकॉर्ड का चयन करें

  5. ध्यान में न आया त्रुटि:अपरिभाषित समारोह के लिए कॉल mysql_connect ()