这里写自定义目录标题
import requests
from bs4 import BeautifulSoup
import re
import TestData
def getContent():
headers = {
“User-Agent”: “Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36 Edg/90.0.818.66”,
‘Connection’: ‘keep-alive’
}
url = “https://movie.douban.com/top250”
r = requests.get(url, headers=headers)
#soup = BeautifulSoup(r.text,“html.parser”)
soup = BeautifulSoup(TestData.aa,“html.parser”)
return soup
def getItem(soup):
movieList = soup.find(“ol”,attrs = {“class”:“grid_view”})
liList = movieList.find_all(“li”)
dataList = []
for li in liList:
name = li.find("span",attrs={"class":"title"}).string
star = li.find("div",attrs={"class":"star"})
num = re.findall(r'(\d+)',str(star))[-1]
rating_num = star.find("span",attrs = {"class":"rating_num"}).string
inq = li.find("span",attrs={"class":"inq"}).string
print("%s → %s → %s → %s"%(name,num,rating_num,inq))
dataList.append((name,num,rating_num,inq))
return dataList
soup = getContent()
getItem(soup)