爬取汽车之家新闻,代码以下html
import requests res=requests.get(url='https://www.autohome.com.cn/news/') #向汽车直接发起get请求,获取请求数据 res.encoding=res.apparent_encoding #把html的编码方式指定给res,避免编码方式不匹配乱码 from bs4 import BeautifulSoup soup=BeautifulSoup(res.text,'html.parser') div=soup.find(name='div',id="auto-channel-lazyload-article") #获取id为'auto-channel-lazyload-article'的div标签 li_list=div.find_all(name='li') #获取全部的li标签,生成列表,而后遍历获取每隔li标签的数据 for li in li_list: h3=li.find(name='h3') if h3:#若是h3标签不存在后面的代码会报错,故如h3标签为空,则跳过 print(h3.text) #获取h3标签的文本 p = li.find(name='p') print(p.text)#获取p标签的文本 #获取li标签中的a标签,获取href并剔除// a = li.find(name='a') href=a.get('href') href_url=href.split("//")[1] print(href_url) print(" " * 20)
自动登陆抽屉,并点赞json
# 该url登陆及点赞操做均需携带登陆前的cookie,故get请求后先获取cookie import requests from bs4 import BeautifulSoup #向url发起请求,获取cookie res=requests.get( url='https://dig.chouti.com/', headers={'user-agent':"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36"} ) res_cookie=res.cookies.get_dict() #因为需点赞该页面全部新闻,需获取全部新闻的url的id.该id在class为discus-a的a标签中,故先获取全部的a标签,便于后续遍历获取id soup=BeautifulSoup(res.text,'html.parser') a_list=soup.find_all(name='a',attrs={'class':'discus-a'}) # 登陆抽屉 login=requests.request( url='https://dig.chouti.com/login', method='POST', headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36'}, data={'phone':'8618857172792', 'password':'Z123456z@', 'oneMonth':'1', }, cookies=res_cookie ) #遍历获取id点赞 for a in a_list: id=a.get('lang') res=requests.request( method='POST', url='https://dig.chouti.com/link/vote?linksId='+id, headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36'}, cookies=res_cookie, ) print(res.text) print('*'*20)
爬虫本质:编写程序,模拟浏览器发送请求获取网站信息.requests请求中常见参数参数: method:网络请求方式.如get/post. url:请求的域名/ip地址 heards:请求头.例 headers={ 'user-agent': "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36"} #user-agent指请求的终端信息 cookies:cookie params:url中传参数 如:params={'user':'tom','pwd':'123'} #同等于http://www.xxx.com?user=tom&pwd=123, data:请求体中传值 json:转换请求体中的格式: 如data={'user':'tom','pwd':'123'},请求体中数据为user=tom&pwd=123,json转换后为"{'user':'tom','pwd':'123'}" data=json.dumps{'user':'tom','pwd':'123'}效果同等于json={'user':'tom','pwd':'123'} files:文件参数.例: file_dict={ 'f1':('新的文件名',open('文件名','rb')) #参数2可传文件句柄或文件内容 } files=file_dict auth:基本的认证方式 (不多用,经常使用于弹窗认证登陆) 例 from requests.auth import HTTPBasicAuth,HTTPDigestAuth ret=requests.get( url='', auth=HTTPBasicAuth('tom','123456') ) print(ret.text) timeout:超时时间,例 ret=request.get(url='www.***.com',timeout=(10,1))#参数1是响应时间最多10秒,参数2是请求时间最多等1秒,超时后则中止 allow_redirects:是否重定向 proxies:代理ip 例 proxies={'http':'**.**.**.**','https':'**.**.**.**'} #访问 http使用**ip;访问https,使用**ip proxies={'http://**.**.**.**':'http://**.**.**.**:**'}# 访问**ip,使用**代理 注:如代理须要使用用户名密码,则需导入HTTPProxyAuth. from requests.auth import HTTPProxyAuth proxies_dict={'http':'**.**.**.**','https':'**.**.**.**'} auth=HTTPProxyAuth{'user','passwd'} res=requests.get(url='',proxies=proxies_dict,auth=auth) print(res.text) stream:下载大文件时候使用,相似迭代器的上下文管理.例 1. res=requests.get(url='https://www.autohome.com.cn/news/') for i in res.iter_content(): print(i) 2. form contextlib importan closing with closing(requests.get('https://www.autohome.com.cn/news/',stream=True)) as r; for i in r.iter_content(): print(i) cert:证书(本质是对数据加密),如https和http的区别 verify:在证书验证的过程当中进行确认例: import requests requests.get( url="http://www.xxx.com", params={'user':'tom','pwd':'123'} #同等于http://www.xxx.com?user=tom&pwd=123, heards={}, cookies={} ) requests.post( url="http://www.xxx.com", params={'user':'tom','pwd':'123'} #同等于http://www.xxx.com?user=tom&pwd=123, heards={}, cookies={}, data={}, #get请求中没有请求体,故没有data )