前言:
目前兄弟们对“猫眼电影评论爬虫”大体比较关切,各位老铁们都想要剖析一些“猫眼电影评论爬虫”的相关内容。那么小编也在网摘上汇集了一些对于“猫眼电影评论爬虫””的相关文章,希望朋友们能喜欢,你们快快来学习一下吧!如果你正在学习爬虫,本文是比较好的选择,建议在学习的时候打开猫眼电影top100进行标签的选择,具体分析步骤就省略啦,具体的方法可看我以前的总结。
练习猫眼top100是为了复习request和beautifulsoup以及sql中表的创建、字段类型的设置。好了附上源代码
import requestsfrom bs4 import BeautifulSoupfrom requests.exceptions import RequestExceptionheaders = { '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"}def get_page_index(url): try: respones = requests.get(url, headers=headers) # get请求 if respones.status_code == 200: return respones.text return None except RequestException: print("请求有错误") return Nonedef parse_one_page(html): soup = BeautifulSoup(html, 'lxml') board_wrapper = soup.find_all('dl', class_='board-wrapper')[0] board_index = board_wrapper.find_all('dd') for link in board_index: title = link.find_all('p', class_='name')[0].text board_index = link.find_all('i')[0].text star = link.find_all('p', class_='star')[0].text.strip()[3:] releasetime = link.find_all('p', class_='releasetime')[0].text.strip()[5:] score = link.find_all('p', class_='score')[0].text print(board_index, title, star, releasetime, score) def main(): t = [0, 10, 20, 30, 40, 50, 60, 70, 80, 90] urls = ['{}'.format(i) for i in t] for url in urls: html = get_page_index(url) parse_one_page(html)if __name__ == "__main__": main()爬取内容如下:存入数据库
存入数据库01:新建表ALTER TABLE maoyantop100 CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;create table maoyantop100(board_index int not null,title varchar(20) not null,star varchar(50) not null,releasetime varchar(50) not null,score float not null);
存入数据库02import pymysqlimport requestsimport jsonfrom bs4 import BeautifulSoupfrom requests.exceptions import RequestExceptionheaders = { '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"}def get_page_index(url): try: respones = requests.get(url, headers=headers) # get请求 if respones.status_code == 200: return respones.text return None except RequestException: print("请求有错误") return Nonedef parse_one_page(html): soup = BeautifulSoup(html, 'lxml') board_wrapper = soup.find_all('dl', class_='board-wrapper')[0] board_index = board_wrapper.find_all('dd') for link in board_index: title = link.find_all('p', class_='name')[0].text board_index = link.find_all('i')[0].text star = link.find_all('p', class_='star')[0].text.strip()[3:] releasetime = link.find_all('p', class_='releasetime')[0].text.strip()[5:] score = link.find_all('p', class_='score')[0].text return board_index, title, star, releasetime, score def save_to_mysql(board_index,title,star, releasetime,score): cur = conn.cursor() #用来获得python执行Mysql命令的方法,操作游标。cur = conn.sursor才是真的执行 insert_data = "INSERT INTO maoyantop100(board_index,title,star, releasetime,score)" "VALUES(%s,%s,%s,%s,%s)" val = (board_index,title,star, releasetime,score) cur.execute(insert_data, val) conn.commit()conn=pymysql.connect(host="localhost",user="root", password='180****3940',db='58yuesao',port=3306, charset="utf8")def main(): t = [0, 10, 20, 30, 40, 50, 60, 70, 80, 90] urls = ['{}'.format(i) for i in t] for url in urls: html = get_page_index(url) board_index, title, star, releasetime, score = parse_one_page(html) save_to_mysql(board_index, title, star, releasetime, score)if __name__ == "__main__": main()
本文存在一个小bug,在存入数据库时很崩溃,只爬取了每页的第一条。
感谢阅读!!!
版权声明:
本站文章均来自互联网搜集,如有侵犯您的权益,请联系我们删除,谢谢。
标签: #猫眼电影评论爬虫