day 3-数据接口和selenium基础
1.头条热搜数据接口
import requests
url = 'https://www.toutiao.com/hot-event/hot-board/?origin=toutiao_pc&_signature=_02B4Z6wo00f01CXsvRQAAIDDsTrmGY3d-owlyLmAAGvQfVEUvSZXHWWX51Yu5mR2FrzIN9H9XFtAvojAuS2-tiIR7k8DPgQSzy2j8g0E4EmTogbDi-82CRSzGYqBIS-xXKXSH1J51RUNlPXA9f'
response = requests.get(url)
for x in response.json()['data']:
print(x['Title'])
2.selenium基础用法
from selenium.webdriver import Chrome
import time
1.创建浏览器
b = Chrome()
2.打开网页
b.get('https://search.jd.com/Search?keyword=%E9%B8%A1%E7%88%AA&enc=utf-8&wq=%E9%B8%A1%E7%88%AA&pvid=7fd65567ffdd4eaca06cc2d5330af6bc')
3.获取网页源代码
html = b.page_source
print(html)
time.sleep(10)
b.close()
3.selenium基本操作
b = Chrome()
b.get('https://www.jd.com/')
1.输入框输入内容
1)找到输入框
search = b.find_element_by_id('key')
2)输入内容
search.send_keys('手机')
search.send_keys('\n')
2.点击搜索按钮
1)获取点击对象对应标签
search_btn = b.find_element_by_css_selector('#search>div>div.form>button')
2)点击标签
search_btn.click()
time.sleep(2)
next_btn = b.find_element_by_class_name('pn-next')
next_btn.click()
input('关闭:')
b.close()