import requests from bs4 import BeautifulSoup url = 'https://weibo.com/u/1670659923' headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36' } cookies = { 'Cookie': '1mN2r5kExwviijjaUAMJq3Ks' } response = requests.get(url, headers=headers) soup = BeautifulSoup(response.text, 'html.parser') #获取名称 name = soup.find('div', {'class': 'detail_wbtext_4CRf9'}) print(name) 执行以上代码打印结果是none,为什么获取不到内容,请修改代码
时间: 2024-01-14 20:03:08 浏览: 151
import sys import os import urllib from bs4 import BeautifulSoup
可能是由于您使用的是微博网页版,需要先登录才能访问该页面。您可以尝试将 cookies 参数修改为您登录后的 cookies,例如:
```
cookies = {
'SUB': '_2A25N0lKqDeRhGeNL6VYU-S7IzT2IHXVvz6k2rDV6PUJbkdAKLWz6kW1NR8QmJomZ1ZYZo5G2vSvZ2x1UIQ6pQjD',
'SUBP': '0033WrSXqPxfM725Ws9jqgMF55529P9D9W5cXyZJyYyI-1bPbZ5aHJ7N5JpX5KMhUgL.Foq4eoB0nN1Kz2dJLoI0YLxKqL1K-LB--LxKBLB.BLBK-2LxKBLBonL1K-LxK-LBKBL1-2LxK-LBKzL1KMLxK-L1K5L1K-LxK-L1K-L1KqLxK-LB.eL1h5t; ALF=1650968552; SSOLoginState=1629432552; _T_WM=63878901031; WEIBOCN_FROM=1110006030; MLOGIN=1; XSRF-TOKEN=IvCp1vH5bJpMqFr4glZz2mKp
}
```
另外,微博的页面结构可能会不断变化,您可以尝试使用其他方式查找名称元素,例如:
```
name = soup.find('div', {'class': 'pf_username'})
```
阅读全文