import requests
import re
from bs4 import BeautifulSoup
def get_page(page):
url = 'https://maoyan.com/board/4?offset=' + str(page)
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3551.3 Safari/537.36"}
response = requests.get(url, headers=headers)
html = response.text
return html
def parse_info(html):
soup = BeautifulSoup(html, 'lxml')
sort = [s.string for s in soup.find_all(class_=re.compile("board-index board-index"))]
stars = [star.string.strip().replace("主演:", "") for star in soup.find_all(class_="star")]
names = [name.string.strip() for name in soup.find_all(class_="name")]
releasetimes = [time.string.strip().replace("上映时间:", "") for time in soup.find_all(class_="releasetime")]
scores = []
for s in soup.find_all(class_="score"):
score = s.find(class_="integer").string+s.find(class_="fraction").string
scores.append(score)
return list(zip(sort, names,stars,releasetimes,scores))
def save_data(info):
with open('movie.txt', 'a', encoding='utf-8') as f:
for item in info:
f.write(str(item) + '\n')
for page in range(10):
print("正在获取第{}页信息。。。".format(page+1))
html = get_page(page*10)
info = parse_info(html)
save_data(info)