python爬虫爬取豆瓣电影top250使用正则表达式
时间: 2023-12-15 07:32:56 浏览: 157
网络爬虫-通过正则表达式快速获取电影的下载地址-Python实例源码.zip
以下是使用正则表达式爬取豆瓣电影top250的Python爬虫示例代码:
```python
import re
import requests
from bs4 import BeautifulSoup
def get_movies():
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'}
movie_list = []
for i in range(0, 10):
link = 'https://movie.douban.com/top250?start=' + str(i * 25)
r = requests.get(link, headers=headers, timeout=10)
soup = BeautifulSoup(r.text, "html.parser")
div_list = soup.find_all('div', class_='hd')
for each in div_list:
movie = each.a.span.text.strip()
movie_list.append(movie)
return movie_list
def main():
movies = get_movies()
for movie in movies:
print(movie)
if __name__ == '__main__':
main()
```
阅读全文