python爬虫豆瓣评论实验报告_豆瓣爬虫实践-python版

豆瓣登录,无验证码版:

import requests

#starturl = "https://www.douban.com/accounts/login"

loginurl = "https://accounts.douban.com/login"

headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36',

'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',

}

fromdata ={'source':'None',

'redir':'https://shanghai.douban.com/',

'form_email':'yourAccount',

'form_password':'password',

'login':'登录'}

s = requests.Session()

s.headers.update(headers)

resp = s.post(loginurl,fromdata)

with open('douban.html','wb') as f:

f.write(resp.text.encode('utf-8'))

print(resp.status_code)

print(resp.cookies)

s.close()

豆瓣TOP250电影爬虫

import requests

from bs4 import BeautifulSoup

def getContent(bsItem):

content=[]

content.append(item.find('a')['href'])

film=item.find_all('span',{'class':'title'})

film[0]=film[0].string

if len(film) > 1:

film[1]=film[1].string.replace(u'\xa0','').replace(r'/','')

else:

film.append('无外语名')

content.append(film)

content.append(item.find('span',{'class':'rating_num'}).string)

content.append(item.find('span',{'class':'','property':''}).string)

return content

starturl = 'https://movie.douban.com/top250'

headers = {'User-Agent':'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36',

'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',

}

params={'start':0}

s = requests.Session()

s.headers.update(headers)

curpage = 0

with open('doubanfilm.txt','w',encoding='utf-8') as f:

while(curpage<250):

params['start'] = curpage

resp = s.get(starturl,params=params)

bs = BeautifulSoup(resp.text,'html.parser')

for item in bs.find_all('div',{"class":'info'}):

f.write(str(getContent(item))+'\n')

curpage += 25

print('bug end')

s.close()

python,生活因你而精彩!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值