爬虫--selenium

爬虫--selenium,第1张

from selenium.webdriver import Chrome
from selenium.webdriver.common.keys import Keys
import time

web = Chrome()

web.get("http://lagou.com")

# 找到某个元素,去点击
web.find_element_by_xpath('//*[@id="changeCityBox"]/ul/li[1]/a').click() # 点击事件

# 肯能程序比浏览器加载的快,所以有一定的延迟
time.sleep(1)

# 找到输入框,输入文字  , 输入回车
web.find_element_by_xpath('//*[@id="search_input"]').send_keys('python',Keys.ENTER)

time.sleep(1)
# 查找数据位置,进行提取
data_list = web.find_elements_by_xpath('//*[@id="jobList"]/div[1]/div')  # 注意elements

for item in data_list:
    money = item.find_element_by_xpath('./div[1]/div[1]/div[2]/span').text
    print(money)
    # time.sleep(0.5)
from selenium.webdriver import Chrome
from selenium.webdriver.common.keys import Keys
import time

web = Chrome()

web.get("http://www.lagou.com")

# 找到某个元素,去点击
web.find_element_by_xpath('//*[@id="changeCityBox"]/ul/li[1]/a').click() # 点击事件

# 肯能程序比浏览器加载的快,所以有一定的延迟
time.sleep(1)

# 找到输入框,输入文字  , 输入回车
web.find_element_by_xpath('//*[@id="search_input"]').send_keys('python',Keys.ENTER)\

# 如何让进行新窗口切换,selenium,不会自动切换的

time.sleep(2)
web.find_element_by_xpath('//*[@id="jobList"]/div[1]/div[1]/div[1]/div[1]/div[1]/a').click()
# 但是现在窗口仍然是原来的窗口,所以我们要进行切换
web.switch_to.window(web.window_handles[-1])

# 在新窗口提取数据,注意现在窗口已经切换过来了
data = web.find_element_by_xpath('//*[@id="job_detail"]/dd[1]/p').text
print(data)

time.sleep(3)

# 关掉子窗口
web.close()
# 变回原来窗口
web.switch_to.window(web.window_handles[0])

time.sleep(2)
# 继续切换
web.find_element_by_xpath('//*[@id="jobList"]/div[1]/div[3]/div[1]/div[1]/div[1]/a').click()
#  定位下拉列表
import time

from selenium.webdriver import Chrome
from selenium.webdriver.support.select import Select

web = Chrome()

web.get("https://www.endata.com.cn/BoxOffice/BO/Year/index.html")

# 定位到下拉列表
sel_el = web.find_element_by_xpath('//*[@id="OptionDate"]')
# 对元素进行包装,包装成下拉菜单
sel = Select(sel_el)
# 让浏览器进行调整选项 # 索引
for i in range(len(sel.options)):  # 下拉框的所有选项
    sel.select_by_index(i)
    time.sleep(2)
    table = web.find_element_by_xpath('//*[@id="TableList"]/table/tbody/tr[1]/td[2]/a/p').text
    print(table)
    print('='*100)
from selenium.webdriver import Chrome
from selenium.webdriver.chrome.options import Options
import time

# 可以不用显示浏览器,并可以得到源码
opt = Options()
opt.add_argument("--headless")
opt.add_argument("--disbale-gpu")

web = Chrome(options=opt)

web.get('https://www.endata.com.cn/BoxOffice/BO/Year/index.html')

print(web.page_source)
web.close()

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/571434.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-09
下一篇 2022-04-09

发表评论

登录后才能评论

评论列表(0条)

保存