爬取豆瓣電影py_sqlserver

近期因c#項目需要sqlserver大量數據  準備python爬點數據 

 

import requests
from bs4 import BeautifulSoup  # pip install beautifulsoup4


url = 'https://movie.douban.com/top250?start=0&filter='

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36'
}
response = requests.get(url=url, headers=headers)

html = response.text

mov_list=[]
with open('douban.txt', 'w', encoding='utf-8') as file:
    soup = BeautifulSoup(html, 'html.parser')
    # fp.write(name+':'+score+'\n')
    items = soup.find_all(class_="item")
    for i in items:
        title = i.find(class_='title').text
        playable = i.find(class_='playable')
        if playable: playable=playable.text
        bd=i.find(class_='bd').text.split()[1]
        star = i.find(class_='star').text.split()[0]
        inq = i.find(class_="inq").text
        print(title, '\n', playable, '\n',bd,'\n',star,'\n', inq)
        print('================')
        mov_list.append({'title':title,'bd':bd})

for x in mov_list:
    pass  #連接數據庫寫入

    break




#mysql示例       https://www.cnblogs.com/lin135/p/7807731.html
#sqlserver 示例 :https://blog.csdn.net/lin_strong/article/details/82868160

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章