1. 程式人生 > >python 2.7 將網頁上的資料下載到資料庫

python 2.7 將網頁上的資料下載到資料庫

#coding = utf-8
import requests
import pymysql
import re
import os
from bs4 import BeautifulSoup
from time import sleep

def main():
    conn = pymysql.connect(
        host = '127.0.0.1',
        user =  'root',
        passwd = 'root',
        db = 'test',
        charset = 'utf8'
     )
    car = conn.cursor()
    url = "http://jandan.net/2018/10/31/best-astronomy-pictures.html"
    html = requests.get(url).text
    soup = BeautifulSoup(html,'lxml')
    print html
    urllist = soup.find_all('p')
    for l in urllist:
        ul = l.find_all('a')
        for t in ul:
            img_url = t['href']
            dre_url = '\'' + img_url + '\''
            car.execute("insert into href(href) values ("+dre_url+")")
            conn.commit()
    car.close()
    conn.close()
    print "11111111"

if __name__ == "__main__" :
    main()