爬虫
请问为什么运行以后没有反应啊{:9_225:}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 strat in self.staarnum:
html=requests.get(self.url,params={'start':start},headers=self.header)
soup=BeautifulSoup(html,'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()
谢谢大佬们了!!! 你实例化了一个类对象,实例化完会自动调用类对象的初始化函数def __init__(self)
初始化函数就做了些赋值操作,没有后续的。你想要什么反应???
建议先把基本知识了解,再去学爬虫
错误地方有三个:
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()
页:
[1]