lichanglin1987 发表于 2020-6-30 17:03:09

python find_all()方法提取信息之后返回了空列表?

import requests
from bs4 import BeautifulSoup
url = 'http://www.imdb.cn/IMDB250/'
headers = {'user-agent':'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko)Chrome/55.0.2883.87 UBrowser/6.2.4098.3 Safari/537.36',
         'cookie':'SRCHD=AF=NOFORM; SRCHUID=V=2&GUID=98D4053E7AB44716A26EE6999CFEA64B&dmnchg=1; SRCHUSR=DOB=20200630; _EDGE_S=F=1&SID=3449382E0E086B1B052536D70F266A48; _EDGE_V=1; MUIDB=09CAC16B8A856F5F3976CF928BAB6EA0; _SS=SID=3449382E0E086B1B052536D70F266A48&bIm=Daz; ipv6=hit=1593485414774&t=4; SRCHHPGUSR=CW=1042&CH=754&DPR=1&UTC=480&DM=3&WTS=63729078609&HV=1593482177',
         'accept-language':'zh-CN,zh;q=0.8',
         'accept-encoding':'gzip, deflate, br',
         'cache-control':'max-age=0'
         }

res = requests.get(url=url,headers=hearders)
soup = BeautifulSoup(res.text,'html.parser')
dvis = soup.find_all('today',class_='rl_lister-list')
print(dvis)


为啥返回的是个空列表?是哪里出问题了吗

lichanglin1987 发表于 2020-6-30 17:11:36

找错标签了,找到问题了

liuzhengyuan 发表于 2020-6-30 17:11:50

“检查”不太准确
最准确的是把源码下载下来,然后在查路径

因为检查有时候会改变原码

comeheres 发表于 2020-6-30 17:12:39

正确如下,主要是标签写错了,是tbody,而不是today

import requests
from bs4 import BeautifulSoup
url = 'http://www.imdb.cn/IMDB250/'
headers = {'user-agent':'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko)Chrome/55.0.2883.87 UBrowser/6.2.4098.3 Safari/537.36',
         'cookie':'SRCHD=AF=NOFORM; SRCHUID=V=2&GUID=98D4053E7AB44716A26EE6999CFEA64B&dmnchg=1; SRCHUSR=DOB=20200630; _EDGE_S=F=1&SID=3449382E0E086B1B052536D70F266A48; _EDGE_V=1; MUIDB=09CAC16B8A856F5F3976CF928BAB6EA0; _SS=SID=3449382E0E086B1B052536D70F266A48&bIm=Daz; ipv6=hit=1593485414774&t=4; SRCHHPGUSR=CW=1042&CH=754&DPR=1&UTC=480&DM=3&WTS=63729078609&HV=1593482177',
         'accept-language':'zh-CN,zh;q=0.8',
         'accept-encoding':'gzip, deflate, br',
         'cache-control':'max-age=0'
         }

res = requests.get(url=url,headers=headers)
soup = BeautifulSoup(res.text,'html.parser')
dvis = soup.find_all('tbody',class_='rl_lister-list')
print(dvis)

lichanglin1987 发表于 2020-7-1 08:57:20

liuzhengyuan 发表于 2020-6-30 17:11
“检查”不太准确
最准确的是把源码下载下来,然后在查路径



怎么弄

liuzhengyuan 发表于 2020-7-1 09:56:43

lichanglin1987 发表于 2020-7-1 08:57
怎么弄

chorme 的话直接 右键 > “查看网页源代码”

其他浏览器↓
import requests
html = requests.get("网址").text

print(html)
页: [1]
查看完整版本: python find_all()方法提取信息之后返回了空列表?