下面是完整的爬取过程:
import requests
from bs4 import BeautifulSoup
import pandas as pd
from pyecharts import Bar
# 设置代理信息
proxyHost = "www.16yun.cn"
proxyPort = "5445"
proxyUser = "16QMSOML"
proxyPass = "280651"
# 发送HTTP请求,获取热播剧的页面数据
url = "https://m.iqiyi.com/热播剧"
proxies = {
"http": f"http://{proxyUser}:{proxyPass}@{proxyHost}:{proxyPort}",
"https": f"https://{proxyUser}:{proxyPass}@{proxyHost}:{proxyPort}"
}
response = requests.get(url, proxies=proxies)
html = response.text
# 使用BeautifulSoup解析页面
soup = BeautifulSoup(html, "html.parser")
# 提取主题和题材信息
themes = soup.find_all("div", class_="theme")
genres = soup.find_all("div", class_="genre")
# 将数据存储到DataFrame中
data = {"主题": [], "题材": []}
for theme, genre in zip(themes, genres):
data["主题"].append(theme.text)
data["题材"].append(genre.text)
df = pd.DataFrame(data)
# 使用Pandas进行数据处理和分析
theme_counts = df["主题"].value_counts()
genre_counts = df["题材"].value_counts()
# 使用Pyecharts进行数据可视化
bar_theme = Bar("热播好剧主题分布")
bar_theme.add("", theme_counts.index, theme_counts.values)
bar_genre = Bar("热播好剧题材分布")
bar_genre.add("", genre_counts.index, genre_counts.values)
# 展示图表
bar_theme.render("theme.html")
bar_genre.render("genre.html")
最后,我们将使用Pyecharts来创建图表,展示最近热播好剧的主题和题材趋势。我们可以使用柱状图、饼图等图表类型,来直观地展示不同主题和题材的热度和分布情况。