爬虫练习之正则表达式爬取猫眼电影Top100

时间:2023-03-10 07:21:53
爬虫练习之正则表达式爬取猫眼电影Top100
#猫眼电影Top100
import requests,re,time

def get_one_page(url):
headers={
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36'
}
response=requests.get(url,headers=headers)
if response.status_code == 200:
return response.text
return None

def parse_one_page(html):
pattern=re.compile('<dd>.*?board-index.*?>(.*?)</i>.*?title="(.*?)".*?<img data-src="(.*?)".*?<p class="star">(.*?)</p>.*?releasetime">(.*?)</p>.*?integer">(.*?)</i>.*?fraction">(.*?)</i>.*?</dd>',re.S)
items=re.findall(pattern,html)
for i in items:
index,name,url,star,time,score1,score2=i
url=url.strip()
star=star.strip()
time=time.strip()
score=score1.strip()+score2.strip()
print(index,name,url,star,time,score)

def main(offset):
url='https://maoyan.com/board/4?offset='+str(offset)
html=get_one_page(url)
#print(html)
parse_one_page(html)

if __name__=='__main__':
for i in range(10):
main(i*10)