python爬取豆瓣電影top250並儲存至資料庫

2021-10-05 12:34:25 字數 4359 閱讀 1274

#建立douban資料庫

use douabn ;

create table doubantop250(

rank int unsigned primary key not null,

movie_name varchar(

200)

,#名字

grade decimal(10,

1),#評分

quote varchar(

500)

,#短評

*# 建立connection連線 連線物件

conn = connect(host=

"localhost"

, user=

'root'

, password=

'', database=

'douban'

, charset=

'utf8'

)# 獲取cursor物件 游標物件

cs1 = conn.cursor(

)# 執行sql,並返**影響行數

cs1.execute(sql)

print

(cursor.fetchone())

#提交,不然無法儲存新建或者修改的資料

conn.commit(

)# 關閉游標

cs1.close(

)# 關閉連線

conn.close(

)#獲取查詢的結果

result = cs1.fetchone(

)#返回乙個元組 fetchmany()和fetchall()返回的結果是元組套元組

# 如果想要或者字典型別的資料,即通過游標設定引數

cursor=pymysql.cursors.dictcursor:

import requests

from lxml import etree

from pymysql import

*def

getdata()

:# 建立connection連線 連線物件

conn = connect(host=

"localhost"

, user=

'root'

, password=

'', database=

'douban'

, charset=

'utf8'

)# 獲取cursor物件 游標物件

cs1 = conn.cursor(

)#拼接**,發起請求

for x in

range(0

,226,25

):#拼接出來每一頁的位址

url =

"".format

(x)print

("正在爬取--{}--**的資料"

.format

(url)

) kv =

# 更改頭部資訊,對於要求較高的**

response = requests.get(url=url, headers=kv)

# print(response.headers)

html_obj = etree.html(response.text)

div_list = html_obj.xpath(

'//div[@class="item"]'

)for div in div_list:

rank = div.xpath(

'div[1]/em/text()')[

0]movie_name = div.xpath(

'div[2]/div[1]/a/span/text()')[

0]director = div.xpath(

'div[2]/div[2]/p/text()')[

0]year_country = div.xpath(

'div[2]/div[2]/p/text()')[

1]# country=div.xpath('div[2]/div[2]/p/text()')[1].split("/")[0]

grade = div.xpath(

'div[2]/div[2]/div/span[2]/text()')[

0]#replace():把**字元替換成**

comment_num = div.xpath(

'div[2]/div[2]/div/span[4]/text()')[

0].replace(

"人評價",""

)#因為有的電影沒有短評,強行匹配短評會報錯!

try:

short_comment = div.xpath(

'div[2]/div[2]/p[2]/span/text()')[

0]except exception as e:

short_comment=

"沒有短評"

#定義乙個字串,儲存清理過後的電影名稱

movie_name_string =

''for movie in movie_name:

#把電影名稱中的\xa0替換成空,然後把處理過後的電影名稱拼接起來

movie_name_string+=movie.replace(

"\xa0",""

)# \xa0 是不間斷空白符  

# 定義字串,儲存清理過後的年份及國家

movie_year_string=

str(year_country.replace(

'\n',""

).replace(

"\xa0",""

).split(

"/")[0

])movie_country_string=

str(year_country.replace(

'\n',""

).replace(

"\xa0",""

).split(

"/")[1

])movie_category_string=

str(year_country.replace(

'\n',""

).replace(

"\xa0",""

).split(

"/")[2

])# 演員表中會遇到不完整情況

try:

movie_director_string=

str(director.replace(

'\n',""

).split(

'\xa0\xa0\xa0')[

0]).strip(

' ')

#strip()方法從字串中去掉在其左側和右側的空格時,括號內單引號裡應保留空格

python爬取豆瓣電影top250

簡要介紹 爬取豆瓣電影top250上相關電影的資訊,包括影片鏈結 影片名稱 上映時間 排名 豆瓣評分 導演 劇情簡介。使用 requests etree xpath 1 檢視網頁資訊,確定爬取的內容,建立資料庫 class spiderdata peewee.model url peewee.cha...

爬取豆瓣top20電影

import requests from lxml import etree url for i in range 2 url format i 10 訪問目標 response requests.get url url 獲取頁面內容 text html response.text print ht...

爬取豆瓣電影TOP250

利用css選擇器對電影的資訊進行爬取 import requests import parsel import csv import time import re class cssspider def init self self.headers defget dp self,url respon...