这是看书练习出来的,花了好久才弄出换行和排名。新手爬虫的第一条路
import requests
import os
from bs4 import BeautifulSoup
main_url='https://movie.douban.com'
url="https://movie.douban.com/top250?start="
after="&filter="
headers={'user-agent':'Mozilla/5.0'}
m_list=[]
j=1
for i in range(10):
all_url=url+str(i*25)+after
http=requests.get(all_url,headers=headers,timeout=10)
soup=BeautifulSoup(http.text,"lxml")
div_list=soup.find_all('div',class_='hd')
for each in div_list:
movie=each.a.span.text
m_list.append("这是第"+str(j)+"名:"+movie)
j=j+1
print(m_list)
with open("movie_top_250.txt",'a')as f:
for line in m_list:
f.write(str(line)+'\n')
f.close()