Python_scrapy—selenium模拟浏览器进行简单操

2019-01-19  本文已影响0人  Just_do_1995

使用selenium进行浏览器模拟登录

pip install selenium

使用selenium模拟打开浏览器进入淘宝获取商品价格

Chrome:     https://sites.google.com/a/chromium.org/chromedriver/downloads
Edge:       https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/
Firefox:    https://github.com/mozilla/geckodriver/releases
Safari:     https://webkit.org/blog/6900/webdriver-support-in-safari-10/
from selenium import webdriver
from scrapy.selector import Selector

browser = webdriver.Chrome(executable_path="E:/Python/spider_tools/chromedriver_win32/chromedriver.exe") ##该地址为浏览器driver的存储路径
browser.get("https://detail.tmall.com/item.htm?spm=a220m.1000858.1000725.7.65ad796RvVIw8&id=557489349781&skuId=3457692281149&user_id=287745076&cat_id=56588002&is_b=1&rn=7292c4f7b15185093651d0c49ae3e584")

print(browser.page_source)

t_selector=Selector(text=browser.page_source)
print(t_selector.css(".tm-promo-price .tm-price::text").extract())
browser.quit()##退出模拟浏览器

使用selenium模拟浏览器登录知乎

browser.find_element_by_css_selector(".qrcode-signin-container span.signin-switch-password").click()
browser.find_element_by_css_selector(".view-signin input[name='account']").send_keys("账号")
browser.find_element_by_css_selector(".view-signin input[name='password']").send_keys("密码")
browser.find_element_by_css_selector(".view-signin button.sign-button").click()
from selenium import webdriver

browser = webdriver.Chrome(executable_path="E:/Python/spider_tools/chromedriver_win32/chromedriver.exe")
browser.get("https://www.zhihu.com/#signin")

browser.find_element_by_css_selector(".qrcode-signin-container span.signin-switch-password").click()
browser.find_element_by_css_selector(".view-signin input[name='account']").send_keys("账号")
browser.find_element_by_css_selector(".view-signin input[name='password']").send_keys("密码")

browser.find_element_by_css_selector(".view-signin button.sign-button").click()

模拟登录微博

import time
time.sleep(15)
from selenium import webdriver

browser.get("https://weibo.com/")
import time
time.sleep(15)
browser.find_element_by_css_selector("#loginname").send_keys("账号")
browser.find_element_by_css_selector(".info_list.password input[node-type='password']").send_keys("密码")
browser.find_element_by_css_selector("info_list.login_btn a[node-type='submitBtn']").click()

使用selenium模拟鼠标滚动

from selenium import webdriver

browser.get("http://www.oschina.net/blog")
import time
time.sleep(5)

for i in range(3):#循环三次
    browser.execute_script("window.scrollTo(0, document.body.scrollHeight); var lenOfPage=document.body.scrollHeight; return lenOfPage;")
    time.sleep(3)#每次停顿3秒

设置chrome不加载图片

from selenium import webdriver

 chrome_opt = webdriver.ChromeOptions()
prefs = {"profile.managed_default_content_settings.images":2}
chrome_opt.add_experimental_option("prefs", prefs)
browser = webdriver.Chrome(executable_path="E:/Python/spider_tools/chromedriver_win32/chromedriver.exe",chrome_options=chrome_opt)
browser.get("https://www.taobao.com")

phantomjs --无界面浏览器

http://phantomjs.org/download.html
from selenium import webdriver

browser = webdriver.PhantomJS("E:/Python/spider_tools/phantomjs-2.1.1-windows/bin/phantomjs.exe")
browser.get("https://item.taobao.com/item.htm?id=539806403522&ali_refid=a3_430329_1006:1121343672:N:%E6%BD%AE%E6%B5%81%E6%97%B6%E5%B0%9A%E6%89%8B%E8%A1%A8:68ff99fcbd71d7908f312a27127a7bf9&ali_trackid=1_68ff99fcbd71d7908f312a27127a7bf9&spm=a217x.7279425.1997511617.409&scm=1029.minilist-17.1.50005700#detail")
print(browser.page_source)
browser.quit()
上一篇下一篇

猜你喜欢

热点阅读