#1.练习
# (1) 获取网页的源码
# (2) 解析 解析的服务器响应的文件 etree.HTML
# (3) 打印
import urllib.request
url='https://www.baidu.com/'
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36'
}
#请求对象定制
request=urllib.request.Request(url=url,headers=headers)
#模拟浏览器访问服务器
response=urllib.request.urlopen(request)
#获取网页源码
content=response.read().decode('utf-8')
#解析网页源码 来获取想要的数据
from lxml import etree
#解析服务器相应的文件
tree=etree.HTML(content)
#获取想要的数据 xpath的返回值是一个列表类型的数据
result=tree.xpath('//input[@id="su"]/@value')[0]
print(result)#2.练习
# (1) 请求对象的定制
# (2)获取网页的源码
# (3)下载# 需求 下载的前十页的图片
# https://sc.chinaz.com/tupian/qinglvtupian.html 1
# https://sc.chinaz.com/tupian/qinglvtupian_page.html
import urllib.request
from lxml import etree
def create_request(page):if(page==1):url='https://sc.chinaz.com/tupian/qinglvtupian.html'else:url = 'https://sc.chinaz.com/tupian/qinglvtupian_'+str(page)+'.html'headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36',}request=urllib.request.Request(url=url,headers=headers)return request
def get_content(request):response=urllib.request.urlopen(request)content=response.read().decode('utf-8')return content
def down_load(content):下载图片# urllib.request.urlretrieve('图片地址','文件的名字')tree=etree.HTML(content)name_list=tree.xpath('//div[@id="container"]//a/img/@alt')#一般设计网站的图片都会进行懒加载src_list=tree.xpath('//div[@id="container"]//a/img/@src2')for i in range(len(name_list)):name=name_list[i]src=src_list[i]url='https:'+srcurllib.request.urlretrieve(url=url,filename='./loveImg/'+name+'.jpg')
if __name__ == '__main__':strat_page=int(input('请输入起始页码:')end_page=int(input('请输入结束页码:')for i in range(start_page,end_page+1):request=create_request(page)content=get_content(request)down_load(content)
#3.练习json数据格式
{ "store": {"book": [{ "category": "修真","author": "六道","title": "坏蛋是怎样练成的","price": 8.95},{ "category": "修真","author": "天蚕土豆","title": "斗破苍穹","price": 12.99},{ "category": "修真","author": "唐家三少","title": "斗罗大陆","isbn": "0-553-21311-3","price": 8.99},{ "category": "修真","author": "南派三叔","title": "星辰变","isbn": "0-395-19395-8","price": 22.99}],"bicycle": {"author": "老马","color": "黑色","price": 19.95}}
}
import ison
import isonpath
obj=(open('073_尚硅谷_爬虫_解析_jsonpath.json','r',encoding='utf-8'))
#那本书超过了10块钱
book_list=jsonpath.jsonpath(obj,'$..book[?(@.price>10)']
print(book_list)
# 书店所有书的作者
# author_list = jsonpath.jsonpath(obj,'$.store.book[*].author')
# print(author_list)# 所有的作者
# author_list = jsonpath.jsonpath(obj,'$..author')
# print(author_list)# store下面的所有的元素
# tag_list = jsonpath.jsonpath(obj,'$.store.*')
# print(tag_list)# store里面所有东西的price
# price_list = jsonpath.jsonpath(obj,'$.store..price')
# print(price_list)# 第三个书
# book = jsonpath.jsonpath(obj,'$..book[2]')
# print(book)# 最后一本书
# book = jsonpath.jsonpath(obj,'$..book[(@.length-1)]')
# print(book)# 前面的两本书
# book_list = jsonpath.jsonpath(obj,'$..book[0,1]')
# book_list = jsonpath.jsonpath(obj,'$..book[:2]')
# print(book_list)# 条件过滤需要在()的前面添加一个?
# 过滤出所有的包含isbn的书。
# book_list = jsonpath.jsonpath(obj,'$..book[?(@.isbn)]')
# print(book_list)#4.练习json解析
import urllib.request
url= 'https://dianying.taobao.com/cityAction.json?activityId&_ksTS=1629789477003_137&jsoncallback=jsonp138&action=cityAction&n_s=new&event_submit_doGetAllRegion=true'
headers = {# ':authority': 'dianying.taobao.com',# ':method': 'GET',# ':path': '/cityAction.json?activityId&_ksTS=1629789477003_137&jsoncallback=jsonp138&action=cityAction&n_s=new&event_submit_doGetAllRegion=true',# ':scheme': 'https','accept': 'text/javascript, application/javascript, application/ecmascript, application/x-ecmascript, */*; q=0.01',# 'accept-encoding': 'gzip, deflate, br','accept-language': 'zh-CN,zh;q=0.9','cookie': 'cna=UkO6F8VULRwCAXTqq7dbS5A8; miid=949542021157939863; sgcookie=E100F01JK9XMmyoZRigjfmZKExNdRHQqPf4v9NIWIC1nnpnxyNgROLshAf0gz7lGnkKvwCnu1umyfirMSAWtubqc4g%3D%3D; tracknick=action_li; _cc_=UIHiLt3xSw%3D%3D; enc=dA18hg7jG1xapfVGPHoQCAkPQ4as1%2FEUqsG4M6AcAjHFFUM54HWpBv4AAm0MbQgqO%2BiZ5qkUeLIxljrHkOW%2BtQ%3D%3D; hng=CN%7Czh-CN%7CCNY%7C156; thw=cn; _m_h5_tk=3ca69de1b9ad7dce614840fcd015dcdb_1629776735568; _m_h5_tk_enc=ab56df54999d1d2cac2f82753ae29f82; t=874e6ce33295bf6b95cfcfaff0af0db6; xlly_s=1; cookie2=13acd8f4dafac4f7bd2177d6710d60fe; v=0; _tb_token_=e65ebbe536158; tfstk=cGhRB7mNpnxkDmUx7YpDAMNM2gTGZbWLxUZN9U4ulewe025didli6j5AFPI8MEC..; l=eBrgmF1cOsMXqSxaBO5aFurza77tzIRb8sPzaNbMiInca6OdtFt_rNCK2Ns9SdtjgtfFBetPVKlOcRCEF3apbgiMW_N-1NKDSxJ6-; isg=BBoas2yXLzHdGp3pCh7XVmpja8A8S54lyLj1RySTHq14l7vRDNufNAjpZ2MLRxa9','referer': 'https://dianying.taobao.com/','sec-ch-ua': '"Chromium";v="92", " Not A;Brand";v="99", "Google Chrome";v="92"','sec-ch-ua-mobile': '?0','sec-fetch-dest': 'empty','sec-fetch-mode': 'cors','sec-fetch-site': 'same-origin','user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36','x-requested-with': 'XMLHttpRequest',
}
#headers里面的以冒号开头的直接注释掉,一般都不太行
request=urllib.request.Request(url=url,headers=headers)
response=urllib.request.urlopen(request)
content=response.read().decode('utf-8')
content=content.split('(')[1].split(')')[0]
with open ('074_尚硅谷_爬虫_解析_jsonpath解析淘票票.json','w',encoding='utf-8')as fp:fp.write(content)
import json
import jsonpath
obj=json.load(open('074_尚硅谷_爬虫_解析_jsonpath解析淘票票.json','r',encoding='utf-8'))
city_list=jsonpath.jsonpath(obj,'$..regionname')
printy(city_list)#5.bs的练习
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body><div><ul><li id="l1">张三</li><li id="l2">李四</li><li>王五</li><a href="" id="" class="a1">尚硅谷</a><span>嘿嘿嘿</span></ul></div><a href="" title="a2">百度</a><div id="d1"><span>哈哈哈</span></div><p id="p1" class="p1">呵呵呵</p>
</body>
</html>from bs4 import BeautifulSoup
# 通过解析本地文件 来将bs4的基础语法进行讲解
# 默认打开的文件的编码格式是gbk 所以在打开文件的时候需要指定编码
soup=(BeautifulSoup(open('075_尚硅谷_爬虫_解析_bs4的基本使用.html',encoding='utf-8'),'lxml')# 根据标签名查找节点
# 找到的是第一个符合条件的数据
# print(soup.a)
# 获取标签的属性和属性值
# print(soup.a.attrs)# bs4的一些函数
# (1)find
# 返回的是第一个符合条件的数据
# print(soup.find('a'))# 根据title的值来找到对应的标签对象
# print(soup.find('a',title="a2"))# 根据class的值来找到对应的标签对象 注意的是class需要添加下划线
# print(soup.find('a',class_="a1"))# (2)find_all 返回的是一个列表 并且返回了所有的a标签
# print(soup.find_all('a'))# 如果想获取的是多个标签的数据 那么需要在find_all的参数中添加的是列表的数据
# print(soup.find_all(['a','span']))# limit的作用是查找前几个数据
# print(soup.find_all('li',limit=2))# (3)select(推荐)
# select方法返回的是一个列表 并且会返回多个数据
# print(soup.select('a'))# 可以通过.代表class 我们把这种操作叫做类选择器
# print(soup.select('.a1'))# print(soup.select('#l1'))# 属性选择器---通过属性来寻找对应的标签
# 查找到li标签中有id的标签
# print(soup.select('li[id]'))# 查找到li标签中id为l2的标签
# print(soup.select('li[id="l2"]'))# 层级选择器
# 后代选择器
# 找到的是div下面的li
# print(soup.select('div li'))# 子代选择器
# 某标签的第一级子标签
# 注意:很多的计算机编程语言中 如果不加空格不会输出内容 但是在bs4中 不会报错 会显示内容
# print(soup.select('div > ul > li'))# 找到a标签和li标签的所有的对象
# print(soup.select('a,li'))# 节点信息
# 获取节点内容
# obj = soup.select('#d1')[0]
# 如果标签对象中 只有内容 那么string和get_text()都可以使用
# 如果标签对象中 除了内容还有标签 那么string就获取不到数据 而get_text()是可以获取数据
# 我们一般情况下 推荐使用get_text()
# print(obj.string)
# print(obj.get_text())# 节点的属性
# obj = soup.select('#p1')[0]
# name是标签的名字
# print(obj.name)
# 将属性值左右一个字典返回
# print(obj.attrs)# 获取节点的属性
obj=soup.select('#p1')[0]
#下面三个都能打印但更推荐第一个
print(obj.attrs.get('class'))
print(obj.get('class')
print(obj['class'])#6.星巴克练习
import urllib.request
url='https://www.starbucks.com.cn/menu/'
response=urllib.request.urlopen(url)
content=response.read().decode('utf-8')
from bs4 import BeautifulSoup
soup=BeautifulSoup(content,'lxml')
name_list=soup.select('ul[class="grid padded-3 product"] strong')
for name in name_list:print(name.get_text())
#7.练习为什么要学习selenium
#导入selenium
from selenium import webdriver
#创建浏览器对象
path='chromedriver.exe'
browser=webdriver.Chrome(path)
url = 'https://www.jd.com/'
browser.get(url)
content=browser.page_source
print(content)
#8.练习 元素定位
from selenium import webdriver
path ='chromedriver.exe'
browser=webdriver.Chrome(path)
url = 'https://www.baidu.com'
browser.get(url)
# 元素定位# 根据id来找到对象
# button = browser.find_element_by_id('su')
# print(button)# 根据标签属性的属性值来获取对象的
# button = browser.find_element_by_name('wd')
# print(button)# 根据xpath语句来获取对象
# button = browser.find_elements_by_xpath('//input[@id="su"]')
# print(button)# 根据标签的名字来获取对象
# button = browser.find_elements_by_tag_name('input')
# print(button)# 使用的bs4的语法来获取对象
# button = browser.find_elements_by_css_selector('#su')
# print(button)# button = browser.find_element_by_link_text('直播')
# print(button)