利用python爬取百度百科中《平凡的荣耀》中演员信息的代码
时间: 2024-05-09 15:17:53 浏览: 6
以下是可以用来爬取《平凡的荣耀》演员信息的 Python 代码:
```python
import requests
from bs4 import BeautifulSoup
url = 'https://baike.baidu.com/item/%E5%B9%B3%E5%87%A1%E7%9A%84%E8%8D%A3%E8%80%80/20933716'
response = requests.get(url)
soup = BeautifulSoup(response.content, 'html.parser')
tables = soup.find_all('table', attrs={'log-set-param': 'table_view'})
for table in tables:
rows = table.find_all('tr')
for row in rows:
cells = row.find_all('td')
if len(cells) >= 2:
actor = cells[0].get_text().strip()
role = cells[1].get_text().strip()
print(f'Actor: {actor}, Role: {role}')
```
这个代码可以获取《平凡的荣耀》百度百科页面中所有演员的信息,包括演员姓名和角色名称。要使用这个代码,你需要用到 requests 和 BeautifulSoup 这两个 Python 库。