优化这段代码import requests from bs4 import BeautifulSoup head={"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36 Edg/114.0.1823.67" } content = requests.get("https://movie.douban.com/annual/2022?fullscreen=1&source=movie_navigation",headers = head).text soup = BeautifulSoup(content, "html.parser") all_links = soup.findAll('span',attrs={"class": "movie-name-text"}) for link in all_links: all_names = link.findAll('a') for name in all_names: print( name.string)
时间: 2024-04-13 09:24:37 浏览: 156
import sys import os import urllib from bs4 import BeautifulSoup
这段代码已经相对简洁,但还是可以进行一些优化。以下是优化后的代码:
```python
import requests
from bs4 import BeautifulSoup
url = "https://movie.douban.com/annual/2022?fullscreen=1&source=movie_navigation"
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36 Edg/114.0.1823.67"
}
content = requests.get(url, headers=headers).text
soup = BeautifulSoup(content, "html.parser")
all_names = soup.select('span.movie-name-text a')
for name in all_names:
print(name.string)
```
优化说明:
1. 将请求的URL和请求头分别赋值给`url`和`headers`变量,使代码更易读。
2. 使用`soup.select()`方法替代`soup.findAll()`方法,使用CSS选择器来查找电影名称的`a`标签,使代码更简洁。
3. 直接使用`all_names`变量来遍历电影名称,不再需要嵌套循环,使代码更简洁。
这样优化后的代码仍然实现了相同的功能,但更加简洁易读。
阅读全文