学习爬虫第三天,尝试爬取亚运会奖牌榜,但是合计有点问题,有没有大佬指点指点[Python] 纯文本查看 复制代码 import requests
from bs4 import BeautifulSoup
url = 'https://tiyu.baidu.com/major/home/%E6%9D%AD%E5%B7%9E%E4%BA%9A%E8%BF%90%E4%BC%9A/tab/%E5%A5%96%E7%89%8C%E6%A6%9C/from/pc'
response = requests.get(url)
html_content = response.text
soup = BeautifulSoup(html_content, "html.parser")
pm = soup.select("span.title")
gj = soup.select("div.region-text.c-line-clamp1")
jp = soup.select("div.medal-num[style='color:#D59F36;']")
yp = soup.select("div.medal-num[style='color:#7F7F7F;']")
tp = soup.select("div.medal-num[style='color:#FB7F32;']")
hj = soup.select("div.total-num")
data_list = []
for i in range(len(pm)):
data = {
"排名": pm[i].get_text(),
"国家": gj[i].get_text(),
"金牌": jp[i].get_text(),
"银牌": yp[i].get_text(),
"铜牌": tp[i].get_text(),
"合计": hj[i].get_text()
}
data_list.append(data)
for lists in data_list:
print(lists) |