近期因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