python爬虫 豆瓣影评的爬取cookies实现自动登录账号

python爬虫 豆瓣影评的爬取cookies实现自动登录账号

频繁的登录网页会让豆瓣锁定你的账号……
在这里插入图片描述

网页请求

使用cookies来实现的自动登录账号,这里的cookies因为涉及到账号我屏蔽了,具体的cookies获取方法直接可以让浏览器实现自动登录后,在网页请求信息中自己找到。

def askURL(url):
    head = {
        "User-Agent": "Mozilla / 5.0(Windows NT 10.0;WOW64) AppleWebKit / 537.36(KHTML, likeGecko) Chrome / 77.0.3865.90Safari / 537.36"
    }
    cookies ={
        "Cookie":' ***********************'
    }
    # request = urllib.request.Request(url, headers=head)
    # html = ""
    # response = urllib.request.urlopen(request)
    # html = response.read().decode("utf-8")
    html = requests.get(url,cookies=cookies,headers=head)
    print("网站返回成功")
    return html.text

获取数据代码片段

再看豆瓣影评的时候,我发现他的所有评论我没有办法完全获取下来在这里插入图片描述
他这里的评论我没理解错的话应该是31万+的评论,但是实际获取的时候在26页以后就什么都没有了。

re正则表达式

findCritic = re.compile(r'<span class="short">(.*?)</span>',re.S)
findUser = re.compile(r'<a href=.*? title="(.*?)">',re.S)
findScore = re.compile(r'<span class="(.*?)" title=')

具体方法

def getDate(base_url):
    datelist = []
    for i in range(0,25):
        url = base_url +str(i*20)
        html = askURL(url)
        print("第{0}页".format(i+1))
        soup = BeautifulSoup(html,"html.parser")
        for item in soup.find_all('div',class_ = "comment-item"):
            date = []
            item = str(item)
            #print(item)
            user = re.findall(findUser,item)
            date.append(user)
            score = re.findall(findScore, item)[0]
            date.append(score)
            critic = re.findall(findCritic,item)
            date.append(critic)
            datelist.append(date)
    return datelist
数据库保存

这里因为处理用户名中含有单引号的问题给我搞得有点傻,使用str.replace()先把用户名中的单引号变为空格,再将字符串格式的两边双引号变为单引号,最后才满足的数据库插入格式。
如果有大佬有更好的解决办法可以评论区告诉我。

def saveDate_DB(datelist,dbpath):
    init_DB(dbpath)
    conn = sqlite3.connect(dbpath)
    cursor = conn.cursor()
    for date in datelist:
        for index in range(len(date)):
            date[index] = str(date[index])
            date[index] = date[index].replace("'"," ");
            date[index] = date[index].replace('"', "'");
            date[index] = '"'+str(date[index])+'"'
        sql = '''
            insert into bawangbieji(
                author ,score ,critics
            )
            values(%s)'''%",".join(date)
        #print(sql)
        cursor.execute(sql)
        conn.commit()

    conn.close()
    print("保存到数据库",dbpath)
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值