国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

python爬取豆瓣電影TOP250數(shù)據(jù)

瀏覽:59日期:2022-06-18 16:25:24

在執(zhí)行程序前,先在MySQL中創(chuàng)建一個(gè)數(shù)據(jù)庫(kù)'pachong'。

import pymysqlimport requestsimport re#獲取資源并下載def resp(listURL): #連接數(shù)據(jù)庫(kù) conn = pymysql.connect(host = ’127.0.0.1’,port = 3306,user = ’root’,password = ’******’, #數(shù)據(jù)庫(kù)密碼請(qǐng)根據(jù)自身實(shí)際密碼輸入database = ’pachong’, charset = ’utf8’ ) #創(chuàng)建數(shù)據(jù)庫(kù)游標(biāo) cursor = conn.cursor() #創(chuàng)建列表t_movieTOP250(執(zhí)行sql語句) cursor.execute(’create table t_movieTOP250(id INT PRIMARY KEY auto_increment NOT NULL ,movieName VARCHAR(20) NOT NULL ,pictrue_address VARCHAR(100))’) try:# 爬取數(shù)據(jù)for urlPath in listURL: # 獲取網(wǎng)頁(yè)源代碼 response = requests.get(urlPath) html = response.text # 正則表達(dá)式 namePat = r’alt='(.*?)' src=’ imgPat = r’src='https://www.xxx.com.cn/bcjs/(.*?)' class=’ # 匹配正則(排名【用數(shù)據(jù)庫(kù)中id代替,自動(dòng)生成及排序】、電影名、電影海報(bào)(圖片地址)) res2 = re.compile(namePat) res3 = re.compile(imgPat) textList2 = res2.findall(html) textList3 = res3.findall(html) # 遍歷列表中元素,并將數(shù)據(jù)存入數(shù)據(jù)庫(kù) for i in range(len(textList3)):cursor.execute(’insert into t_movieTOP250(movieName,pictrue_address) VALUES('%s','%s')’ % (textList2[i],textList3[i]))#從游標(biāo)中獲取結(jié)果cursor.fetchall()#提交結(jié)果conn.commit()print('結(jié)果已提交') except Exception as e:#數(shù)據(jù)回滾conn.rollback()print('數(shù)據(jù)已回滾') #關(guān)閉數(shù)據(jù)庫(kù) conn.close()#top250所有網(wǎng)頁(yè)網(wǎng)址def page(url): urlList = [] for i in range(10):num = str(25*i)pagePat = r’?start=’ + num + ’&filter=’urL = url+pagePaturlList.append(urL) return urlListif __name__ == ’__main__’: url = r'https://movie.douban.com/top250' listURL = page(url) resp(listURL)

結(jié)果如下圖:

python爬取豆瓣電影TOP250數(shù)據(jù)

python爬取豆瓣電影TOP250數(shù)據(jù)

以上就是我的分享,如果有什么不足之處請(qǐng)指出,多交流,謝謝!

以上就是python爬取豆瓣電影TOP250數(shù)據(jù)的詳細(xì)內(nèi)容,更多關(guān)于python爬取豆瓣電影的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標(biāo)簽: 豆瓣 Python
相關(guān)文章:
主站蜘蛛池模板: 讷河市| 舟曲县| 灵川县| 保康县| 伊川县| 康马县| 永仁县| 南雄市| 喜德县| 丹江口市| 台中县| 汉寿县| 紫金县| 南投县| 城固县| 潮州市| 收藏| 沁水县| 桂东县| 蒙阴县| 呼玛县| 扎囊县| 福鼎市| 芜湖县| 尚志市| 庆云县| 浠水县| 盘山县| 赣州市| 西藏| 塘沽区| 房产| 兴城市| 湟源县| 彰化县| 临江市| 探索| 丰城市| 寿阳县| 东兴市| 开封市|