Pythonnewers 发表于 2021-1-17 13:10:18

爬取b站用户信息

本帖最后由 Pythonnewers 于 2021-1-17 13:14 编辑

RT
可爬取"mid",'名字','性别','头像地址','等级','大会员'
import requests
import re
import json
from bs4 import BeautifulSoup
import xlsxwriter
import time
import random
book = xlsxwriter.Workbook(r'C:\Users\Sun\Desktop\Bilili User.xlsx')
sheet = book.add_worksheet()
word=['A','B','C','D','E','F','G','E']
oppotion=["mid",'名字','性别','头像地址','等级','大会员']
for p in range(len(oppotion)):
    l = word
    sheet.write(f"{l}1",oppotion)
headers={"user-agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36"}

def excel(*x):
    for i in range(len(x)):
      l=word
      sheet.write(f"{l}{ueser+2}",x)
    print(str(x)+"完成")
def mes(x):
    print(x)
    if x['code']==-404:
      return
    x=x['data']
    mid=x["mid"]
    name=x["name"]
    sex=x["sex"]
    face=x["face"]
    level=x["level"]
    vip=x['vip']['label']['text']
    excel(mid,name,sex,face,level,vip)
def req(url):
    res=requests.get(url=url,headers=headers).content.decode("utf-8")
    soup=BeautifulSoup(res,"lxml")
    dic=json.loads(soup.p.get_text())
    mes(dic)
def uid(a):
    mid=re.findall("\d+",a)
    global data
    data={'mid':f'{mid}','jsonp':'jsonp'}
    url=f"https://api.bilibili.com/x/space/acc/info?mid={mid}&jsonp=jsonp"
    req(url)
def user(uesers):
    global ueser
    for ueser in range(uesers):
      url=f"https://space.bilibili.com/{ueser+1}/"
      uid(url)
      time.sleep(random.random())
    book.close()
user(100)
↑↑↑
user这里()写爬取多少用户(按顺序)
↑我有些打英文手滑写错了,运行后才发现拼写错误{:10_266:}
mid应该就是uid
页: [1]
查看完整版本: 爬取b站用户信息