使用Python爬取豆瓣电影Top 250的排名和标题
要爬取豆瓣前250排名电影标题,可以使用Python的requests库和BeautifulSoup库。首先,需要安装这两个库: ```bash pip install requests pip install beautifulsoup4 ``` 接下来,编写代码: ```python import requests from bs4 import BeautifulSoup def get_movie_titles(): url = "https://movie.douban.com/top250" headers = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3" } response = requests.get(url, headers=
Python爬取豆瓣前250排名电影标题,通过pip install requests安装requests库 。
话不多说,直接上代码:
importrequestsimportreheaders={"User-Agent":"Mozilla/5.0(WindowsNT10.0;Win64;x64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/115.0.0.0Safari/537.36Edg/115.0.1901.188"}foriinrange(0,226,25):zxcvzx=f"https://movie.douban.com/top250?start={i}&filter="response=requests.get(zxcvzx,headers=headers)a=re.findall("<imgwidth=\"100\"alt=\"(.*?)\"",response.text,re.S)forvbnina:print(vbn)