DAY-02
爬取豆瓣网站电影top250
1 """ 2 请求头: 3 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36 4 5 cookie:
bid=EU-warEMqbg; douban-fav-remind=1; ap_v=0,6.0; _pk_id.100001.4cf6=1a1adee44f5ddbaf.1562026761.1.1562026761.1562026761.; _pk_ses.100001.4cf6=*; __utma=30149280.803503068.1543901733.1543901733.1562026761.2; __utmb=30149280.0.10.1562026761; __utmc=30149280; __utmz=30149280.1562026761.2.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __utma=223695111.1484969385.1562026761.1562026761.1562026761.1; __utmb=223695111.0.10.1562026761; __utmc=223695111; __utmz=223695111.1562026761.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none) 6 """
7 """ 8 电影名称/导演/主演/年份/类型/评分/评论/简介 9 电影url 10 1.分析所有主页的url 11 第一页:https://movie.douban.com/top250?start=0&filter= 12 第二页:https://movie.douban.com/top250?start=25&filter= 13 """ 14 import requests 15 import re 16 # 1.发送请求 17 def get_page(url): 18 response = requests.get(url) 19 return response 20 # 2.解析数据 21 def parse_index(html): 22 movie_list = re.findall('<div class="item">.*?<em class="">(.*?)</em>.*?<a href="(.*?)">.*?<span class="title">(.*?)</span>.*?导演:(.*?)主演:(.*?)<br>(.*?)</p>.*?<span class="rating_num".*?>(.*?)</span>.*?<span>(.*?)人评价</span>.*?<span class="inq">(.*?)</span>',html,re.S) 23 return movie_list 24 25 # 3.保存数据 26 def save_data(movie): 27 top, m_url, name, deractor, actor, year_type, point, commit, desc = movie 28 year_type = year_type.replace('\n','').replace(' ','').replace('/','').strip(' ') 29 deractor = deractor.replace(' ','') 30 data= f""" 31 ======================= 32 电影排名:{top} 33 电影url:{m_url} 34 电影名称:{name} 35 电影导演:{deractor} 36 电影主演:{actor} 37 年份类型:{year_type} 38 电影评分:{point} 39 电影评论:{commit} 40 电影简介:{desc} 41 ======================== 42 \n 43 """ 44 print(data) 45 with open('douban_top250.txt','a',encoding='utf-8') as f: 46 f.write(data) 47 print(f'电影:{name}写入成功...') 48 49 50 if __name__ == '__main__': 51 num = 0 52 for line in range(10): 53 url = f'https://movie.douban.com/top250?start={num}&filter=' 54 num+=25 55 print(url) 56 57 #往每个主页发送请求 58 index_respoonse = get_page(url) 59 60 #解析电影详情页获取电影信息 61 movie_list = parse_index(index_respoonse.text) 62 for movie in movie_list: 63 save_data(movie)
1.什么是Selenium
起初是一个自动化测试工具,原理是驱动浏览器执行一些已定好的操作。
爬虫本质上就是模拟浏览器,所以可以使用它来做爬虫
2.为什么要使用Selenium
优点:
——执行js代码
——不需要分析复杂的通信流程
——对浏览器做弹窗、下拉等操作
——获取动态数据
——破解登入验证
缺点:
——执行效率低
3.安装与使用
1.pip install selenium
2.必须安装浏览器
3.安装浏览器驱动
课堂实例:
selenium的基本使用
1 from selenium import webdriver 2 from selenium.webdriver import ActionChains # 破解滑动验证码的时候用的 可以拖动图片 3 from selenium.webdriver.common.by import By # 按照什么方式查找,By.ID,By.CSS_SELECTOR 4 from selenium.webdriver.common.keys import Keys # 键盘按键操作 5 from selenium.webdriver.support import expected_conditions as EC # 和下面WebDriverWait一起用的 6 from selenium.webdriver.support.wait import WebDriverWait # 等待页面加载某些元素 7 import time 8 9 # 方式一:通过驱动打开浏览器 10 # driver = webdriver.Chrome(r'D:\chromedriver.exe') 11 # 方式二:把webdriver.exe驱动放到python解释器安装目录/scripts 12 driver = webdriver.Chrome() 13 try: 14 driver.get('https://www.jd.com/') 15 #获取显式等待对象10s 16 wait = WebDriverWait(driver,10) 17 18 #查找元素 19 input_tag = wait.until(EC.presence_of_element_located((By.ID,'key'))) 20 21 # 在输入框内输入公仔 22 input_tag.send_keys('公仔') 23 24 #按下回车搜索 25 input_tag.send_keys(Keys.ENTER) 26 27 time.sleep(10) 28 finally: 29 #关闭浏览器释放操作系统资源 30 driver.close()
selenium选择器
1 from selenium import webdriver 2 from selenium.webdriver.common.keys import Keys # 键盘按键操作 3 import time 4 driver = webdriver.Chrome() 5 try: 6 #隐式等待 7 driver.implicitly_wait(10) 8 9 driver.get('https://www.jd.com/') 10 11 ''' 12 ===============所有方法=================== 13 element是查找一个标签 14 elements是查找所有标签 15 ''' 16 # 1、find_element_by_link_text 17 login_button = driver.find_element_by_link_text('登录') 18 login_button.click() #点击登陆按钮 19 # 通过链接文本 20 21 # 2、find_element_by_id 22 user_login = driver.find_element_by_id('TANGRAM__PSP_10__userName') 23 user_login.click() 24 # 通过id去找 25 26 # 3、find_element_by_class_name 27 user = driver.find_element_by_class_name('pass-text-input pass-text-input-userName') 28 user.send_keys('888') 29 30 # 4、find_element_by_partial_link_text 31 login_link = driver.find_element_by_partial_link_text('登') 32 login_link.click() 33 34 # 5、find_element_by_name 35 pwd = driver.find_element_by_name('password') 36 pwd.send_keys('555') 37 submmit = driver.find_element_by_id('TANGRAM__PSP_10__password') 38 submmit.click() 39 40 # 6、find_element_by_css_selector 41 # 根据属性选择器查找元素 42 # .:class 43 # #:id 44 login_link2 = driver.find_element_by_css_selector('.tang-pass-footerBarULogin') 45 login_link2.click() 46 47 # 7、find_element_by_tag_name 48 div = driver.find_element_by_tag_name() 49 print(div) 50 time.sleep(10) 51 finally: 52 #关闭浏览器释放操作系统资源 53 driver.close()