import requests
from bs4 import BeautifulSoup
def get_movies():
headers = {
'user-agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrom/52.0.2743.82 Safari/537.36',
'Host': 'movie.douban.com'
}
movie_list = []
for i in range(0, 10):
link = '' + str(i * 25)
r = requests.get(link, headers=headers, timeout= 10)
print (str(i+1),"页响应状态码:", r.status_code)
soup = BeautifulSoup(r.text, "lxml")
div_list = soup.find_all('div', class_='hd')
for each in div_list:
movie = each.a.span.text.strip()
movie_list.append(movie)
return movie_list
movies = get_movies()
print (movies)#原书中给的是print (movie_list),因为一直觉得书不会错,没有多想,结果发现了自己的低级错误,经过查证,确实是书里面的内容有问题,所以遇到问题要自信,不要盲目啊。
阅读(157915) | 评论(0) | 转发(0) |