用正则表达式爬取猫眼电影top100,具体内容如下
#!/usr/bin/python # -*- coding: utf-8 -*- import json # 快速导入此模块:鼠标先点到要导入的函数处,再Alt + Enter进行选择 from multiprocessing.pool import Pool #引入进程池 import requests import re import csv from requests.exceptions import RequestException #引入异常 ## 正确保存,无丢失 # 请求一个页面返回响应内容 #以《霸王别姬》为列,右击—查看元素—会显示一个网页信息 def get_one_page(url,offset): try: response=requests.get(url=url,params={"offset":offset}) if response.status_code==200: #由状态码判断返回结果,200表示请求成功,300,500表出错 return response.text #返回网页内容 else:return None except RequestException as e: return None # 解析一个页面 def parse_one_page(html): pattern = ('<dd>.*"(.*".*"><a' + '.*?>(.*">(.*">(.*">(.*">(.*"releasetime">上映时间:1993-01-01(中国香港)</p> def get_release_time(data): pattern = '^(.*"\t"参数指写入的时候的分隔符 csv_writer = csv.writer(data_csv) csv_writer.writerow([item['index'], item['image'], item['title'], item['actor'],item['time'],item['area'],item['score']]) # 参数newline是用来控制文本模式之下,一行的结束字符。可以是None,'',\n,\r,\r\n等。 ''''' 也可判断异常,一般没错 try: csv_writer = csv.writer(data_csv) csv_writer.writerow([item['index'], item['image'], item['title'], item['actor'],item['time'],item['area'],item['score']]) except Exception as e: print(e) print(item) ''' # 下载封面图 #读方式打开的话,并不会新建;写方式打开的话就会新建。 r只读,w可写,a追加 def download_thumb(title,image): try: response = requests.get(image) # 获取二进制数据 with open('image/'+title+'.jpg', 'wb') as f: #将封面图保存到当前路径下的image文件夹中,图片名称为:电影名.jpg f.write(response.content) f.close() except RequestException as e: print(e) pass # 主调度程序 def main(): # 起始URL start_url = 'http://maoyan.com/board/4"htmlcode">def main(offset): url='http://maoyan.com/board/4"utf8" try: conn = pymysql.connect(host='localhost', user='root', passwd=' ', port=3306,db='test1',charset="utf8",use_unicode = False ) cur = conn.cursor() # 创建一个游标对象 for item in parse_one_page(html): try: # sql = "INSERT INTO movies (number,picture,title,actors,time,area,score) VALUES (%s,%s,%s,%s,%s,%s,%s)" # cur.execute(sql, ( item['number'],item['picture'],item['title'],item['actors'],item['time'],item['area'],item['score'])) sql = "insert into test_movies (number,picture,title,actors,time,area,score) values(%s,%s,%s,%s,%s,%s,%s)" cur.execute(sql, (item['number'], item['picture'], item['title'], item['actors'], item['time'], item['area'],item['score'])) except pymysql.Error as e: print(e) print('- - - - - 数据保存成功 - - - - -') conn.commit() cur.close() conn.close() # 关闭数据 except pymysql.Error as e: print("Mysql Error %d: %s" % (e.args[0], e.args[1])) if __name__=='__main__': # 连接数据库 conn = pymysql.connect(host='localhost', user='root', passwd=' ', port=3306, db='test1', charset="utf8") cur = conn.cursor() # 创建一个游标对象 cur.execute("DROP TABLE IF EXISTS test_movies") # 如果表存在则删除 # 创建表sql语句 sqlc = """CREATE TABLE test_movies( number int not null primary key auto_increment, picture VARCHAR(100) NOT NULL, title VARCHAR(100) NOT NULL, actors VARCHAR(200) NOT NULL, time VARCHAR(100) NOT NULL, area VARCHAR(100) , score VARCHAR(50) NOT NULL )""" cur.execute(sqlc) # 执行创建数据表操作 pool=Pool() pool.map(main,[i*10 for i in range(10)])以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。