| 
 | 
 
 
发表于 2020-11-13 16:43:26
|
显示全部楼层
 
 
 
 
 
错误地方有三个: 
 
1、定义类,没有调用类中方法 
 
2、for 循环的 start 和 self.starnum 你打错了 
 
3、requests 读取数据后需要 .text 才会获得源码  
 
- import requests
 
 - from bs4 import BeautifulSoup
 
  
- class Douban:
 
 -     def __init__(self):
 
 -         self.url='https://movie.douban.com/top250'
 
 -         self.starnum=[]
 
 -         for start_num in range(0,251,25):
 
 -             self.starnum.append(start_num)
 
 -         self.header={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:82.0) Gecko/20100101 Firefox/82.0'}
 
 -     def get_top250(self):
 
 -         for start in self.starnum:
 
 -             html=requests.get(self.url,params={'start':start},headers=self.header)
 
 -             soup=BeautifulSoup(html.text,'html.parser')
 
 -             num=soup.select('.grid_view > li:nth-child(1) > div:nth-child(1) > div:nth-child(2) > div:nth-child(1) > a:nth-child(1) > span:nth-child(1)')
 
 -             for num in num:
 
 -                 print(num.get_text())
 
 - cls=Douban()
 
 - cls.get_top250()
 
  复制代码 |   
 
 
 
 |