PYthon爬取豆瓣上的数据_简易理解版
本Python相关文章为个人学习过程记录,希望能够给各位提供相关帮助。
import re
import requests
import pymssql
db=pymssql.connect(server='DZAFC-BJB289',database='E_Market',charset='utf8')
cursor=db.cursor()
header={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) \
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36 Edge/18.18362'}
response=requests.get(r'https://movie.douban.com/top250?start=',headers=header).text
findLink=re.compile(r'<a href="(.*?)">') #正则表达式模式的匹配,影片详情、
findImgSrc=re.compile(r'<img.*src="(.*?)"',re.S)#re.S让换行符包含在字符中,图片信息
findTitle=re.compile(r'<span class="title">(.*?)</span>')#影片片名
findRating=re.compile(r'<span class="rating_num" property="v:average">(.*)</span>')#找到评分
findJudge=re.compile(r'<span>(\d*)人评价</span>')#找到评价人数 #\d表示数字
findInq=re.compile(r'<span class="inq">(.*)</span>')#找到概况
findBd=re.compile(r'<p class="">(.*?)</p>',re.S) #找到影片的相关内容,如导演,演员等
findLink1=re.compile(findLink)
findImgSrc1=re.compile(findImgSrc)
findTitle1=re.compile(findTitle)
findRating1=re.compile(findRating)
findJudge1=re.compile(findJudge)
findInq1=re.compile(findInq)
findBd1=re.compile(findBd)
data1=findLink1.findall(response)
data2=findImgSrc1.findall(response)
data3=findTitle1.findall(response)
data4=findRating1.findall(response)
data5=findJudge1.findall(response)
data6=findInq1.findall(response)
data7=findBd1.findall(response)
resultlist=[]
#sqll="truncate table Movie"
#cursor.execute(sqll)
num=100000
db.commit()
for i in range(1000,num):
# resultlist.append(data4[i])
## sql="INSERT INTO Movie(ID,XQ,LJ,ZWM) VALUES (%d,%s,%s,%s)"
# cursor.execute(sql,((i+1),data4[i],data5[i],data6[i]))
sql="INSERT INTO a(a,b,c) VALUES (%d,%s,%s)"
cursor.execute(sql,((i+1),2,3))
db.commit()
db.close()