爬虫学习---基础操作--抽屉新热榜自动点赞与豆瓣自动统一短评
生活随笔
收集整理的這篇文章主要介紹了
爬虫学习---基础操作--抽屉新热榜自动点赞与豆瓣自动统一短评
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
爬蟲的學(xué)習(xí)相對(duì)來說,比較煩鎖,因?yàn)榫W(wǎng)站的反爬規(guī)則,經(jīng)過兩天的研究,終于搞定了抽屜新熱榜自動(dòng)點(diǎn)贊與豆瓣自動(dòng)統(tǒng)一短評(píng)(豆瓣登錄的驗(yàn)證碼目前需要手動(dòng)輸入)
?
抽屜網(wǎng)的規(guī)則問題
示例如下:
1 import requests,re 2 from bs4 import BeautifulSoup 3 headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:57.0) Gecko/20100101 Firefox/57.0' 4 } 5 user='你的帳號(hào)' 6 pwd='你的密碼' 7 def index_first():#抽屜網(wǎng)需要GET的COOKIES 8 r1=requests.get('https://dig.chouti.com/',headers=headers) 9 return r1.cookies.get_dict() 10 def login(cookies,user,pwd): 11 post_dict={ #登陸時(shí)要提交的數(shù)據(jù) 12 'phone':user, 13 'password':pwd, 14 'oneMonth':1 15 } 16 response = requests.post(#要登陸的網(wǎng)址 17 url="https://dig.chouti.com/login", 18 data=post_dict, 19 cookies=cookies,#帶上cookies進(jìn)行授權(quán) 20 headers=headers 21 ) 22 print(response.text,'url') 23 return 24 25 def profiles(cookie_gpsd):#進(jìn)入個(gè)人主頁(yè) 26 response=requests.get( 27 url='https://dig.chouti.com/profile/bind', 28 cookies=cookie_gpsd, 29 headers=headers 30 ) 31 soup =BeautifulSoup(response.text,features='html.parser')#python 內(nèi)置轉(zhuǎn)換的對(duì)象 32 phone=soup.find(id='bind-content') 33 #phone_nube=phone.find('class') 34 reg=r'<div class="show-phoneCode">當(dāng)前綁定號(hào)碼為:(.*?)</div>' 35 rest=re.findall(reg,response.text)#查找 36 print('',rest) 37 if rest[0]=='+你的手機(jī)號(hào)': 38 print('進(jìn)入個(gè)人設(shè)置成功') 39 40 def dz(cookies_dict): 41 ret=requests.post(url='https://dig.chouti.com/link/vote?linksId=18760066',#要點(diǎn)贊的文章地址 42 cookies=cookies_dict, 43 headers=headers) 44 print(ret.text) 45 46 47 cookie_get=index_first()#先取得GET的cookies 48 login(cookie_get,user,pwd)#登陸進(jìn)行cookies的授權(quán) 49 profiles(cookie_get)#可以進(jìn)行后臺(tái) 50 dz(cookie_get)#點(diǎn)贊豆瓣網(wǎng)
1 import requests 2 import re 3 import json 4 from bs4 import BeautifulSoup 5 6 requests =requests.Session()#記錄登錄的狀態(tài) 7 if __name__ == '__main__': 8 user='你的帳號(hào)' 9 pwd='你的密碼' 10 #請(qǐng)求頭 11 headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:57.0) Gecko/20100101 Firefox/57.0' 12 } 13 14 def login(): 15 response=requests.get('https://accounts.douban.com/login',headers=headers) 16 response.encoding=response.apparent_encoding #返回網(wǎng)頁(yè)的原先編碼 17 result=response.text#取出文本即網(wǎng)頁(yè)源碼 18 reg=r'<input type="hidden" name="captcha-id" value="(.*?)"/>'#正則表達(dá)式 19 catpchaId=re.findall(reg,result)#查找 驗(yàn)證碼對(duì)象ID 20 soup =BeautifulSoup(result,features='html.parser')#python 內(nèi)置轉(zhuǎn)換的對(duì)象 21 target=soup.find(id='captcha_image')#查找 驗(yàn)證碼對(duì)象 22 img_url =target.attrs.get('src')#獲取驗(yàn)證碼圖片URL 23 print(img_url,catpchaId) 24 response=requests.get(img_url,headers=headers)#獲取驗(yàn)證碼圖片 25 codeimg=response.content#保存為二進(jìn)制 26 print(codeimg,'codeimg') 27 with open('code.png','wb') as f:#保存為PNG圖片 28 f.write(codeimg) 29 post_dict={ #登陸時(shí)要提交的數(shù)據(jù) 30 'source':'index_nav', 31 'form_email':user,#用戶名 32 'form_password':pwd,#密碼 33 'captcha-solution':input('請(qǐng)輸入驗(yàn)證碼:'),#輸入你保存的PNG圖片中的驗(yàn)證碼 34 'captcha-id':catpchaId 35 } 36 response = requests.post(#要登陸的網(wǎng)址 37 url="https://accounts.douban.com/login", 38 data=post_dict, 39 headers=headers 40 ) 41 print(response.url,'url') 42 # #print(response.text,'text') 43 # cookie_dict=response.cookies.get_dict()#產(chǎn)生的cookie 44 # print(cookie_dict,'cookie_dict') 45 if '莫柔落切的帳號(hào)' in response.text: 46 print('登錄成功') 47 else: 48 print('登錄失敗,重新登錄!') 49 login() 50 return 51 def comment(mid):#評(píng)論 52 data={#評(píng)論的表單數(shù)據(jù)格式 53 'ck':'xVds', 54 'comment':'我來留個(gè)名', 55 'foldcollect':'F', 56 'interest':'collect', 57 'rating':'', 58 'tags':'123' 59 } 60 #進(jìn)行提交 61 response=requests.post('https://movie.douban.com/j/subject/%s/interest'%mid,data=data,headers=headers) 62 print(response.text,'comment.text') 63 def getMoveId():#電影的ID 排行榜 64 response=requests.get('https://movie.douban.com/ithil_j/activity/movie_annual2017/widget/20', 65 headers=headers) 66 print(response.text) 67 result=response.json()#進(jìn)行轉(zhuǎn)換為文本 68 69 print(type(result),'類型') 70 return result['res']['subjects']#返回所要的數(shù)據(jù) 71 72 login() 73 for i in getMoveId():#循環(huán)電影ID列表 74 print(i) 75 comment(i['id'])#進(jìn)行短評(píng)?
轉(zhuǎn)載于:https://www.cnblogs.com/uge3/p/8809044.html
總結(jié)
以上是生活随笔為你收集整理的爬虫学习---基础操作--抽屉新热榜自动点赞与豆瓣自动统一短评的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python实战之数字、日期和时间的高级
- 下一篇: 微信小程序-更改个人信息