python 豆瓣电影 Top 250抓取

豆瓣电影 Top 250抓取

catch抓取网页电影名称地址,函数名称catch

第13-14行:生成url列表

第15-16行:循环抓取网页内容

 1 import requests
 2 from bs4 import BeautifulSoup as bs
 3 headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36(KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36'}
 4 def catch(url):
 5     resp = requests.get(url,headers = headers).text            #get()方法加入请求头
 6     soup = bs(resp,"html.parser")
 7     items = soup.find_all("div",class_="hd")
 8     for i in items:
 9         tag = i.find("a")
10         link = tag['href']
11         name = tag.find(class_="title").text
12         print("名称:%s---地址:%s"%(name,link))
13 url_str='https://movie.douban.com/top250?start={}'
14 url_strs=[url_str.format(num*25) for num in range(10)]
15 for page in url_strs:
16     catch(page)
17     input("press any key...")

第13行:定义一个字符串

第14行:生成字符串列表 https://movie.douban.com/top250?start=0,25,50...

本程序源自蓝桥老师的视频

https://www.ixigua.com/7037126930272879135?id=7037717636230316557&logTag=41e09bfc327e8d355ef0

posted @ 2022-01-29 15:21  paoPaoLong_liu  阅读(42)  评论(0)    收藏  举报