import pymysql
import requests
from lxml import etree
class Douban:
def __init__(self):
self.db = pymysql.connect("127.0.0.1", "root", "1111", "douban")
self.cursor = self.db.cursor()
def forRequest(self):
start = 0
while start <= 225:
requestUrl(self, start)
start = start + 25
def requestUrl(self, start):
url = "https://movie.douban.com/top250"
header = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36"
}
params = {
"start": start,
"filter": ""
}
response = requests.get(url=url, params=params, headers=header).text
terr = etree.HTML(response)
terr_lis = terr.xpath('//ol[@class="grid_view"]/li')
for i in terr_lis:
video_nameT = i.xpath('./div/div/div/a/span/text()')[0]
video_scoreT = i.xpath('./div/div/div/div/span[2]/text()')[0]
video_evaluateT = i.xpath('./div/div/div/div/span[4]/text()')[0]
# print(video_name + ' 评分:' + video_score + "\n")
print("正在往数据库写入" + video_nameT)
#添加语句
sql = 'insert into douban_data(video_name,video_score,video_evaluate) values(%s,%s,%s);'
self.cursor.execute(sql, (video_nameT, video_scoreT, video_evaluateT))
self.db.commit()
if __name__ == "__main__":
Douban().forRequest()