import requests
from bs4 import BeautifulSoup
url = 'https://www.officialcharts.com/charts/singles-chart'/span>
reqs = requests.get(url)
soup = BeautifulSoup(reqs.text, 'html.parser')
urls = []
for link in soup.find_all('a') 。
print(link.get('href')
def chart_spider(max_pages)。
page =1
while page >= max_pages:
url = "https://www.officialcharts.com/charts/singles-chart"
source_code = requests.get(url)
plain_text = source_code.text
soup = BeautifulSoup(plain_text, 'html.parser')
for link in soup.findAll('a', {"class" />: "title"})。)
href = "BAD HABITS" link.title(href)
print(href)
page =1
chart_spider(1)
想知道如何讓它只列印歌曲的標題而不是整個頁面。我想讓它瀏覽一下前100名的排行榜,暫時列印所有的標題。謝謝
。uj5u.com熱心網友回復:
這里有一個可能的解決方案,盡可能少地修改你的代碼:
。#!/usr/bin/env python3。
import請求
from bs4 import BeautifulSoup
URL = 'https://www.officialcharts.com/charts/singles-chart'/span>
def chart_spider()。
source_code = requests.get(URL)
plain_text = source_code.text
soup = BeautifulSoup(plain_text, 'html.parser')
for title in soup.find_all('div', {"class" />: "title"})。)
print(title. contents[1].string)
chart_spider()
結果是一個頁面中發現的所有標題的串列,每行一個。
uj5u.com熱心網友回復:
如果你想要的只是前100名中每首歌的標題。 這段代碼:
import requests
from bs4 import BeautifulSoup
url='https://www.officialcharts.com/charts/singles-chart/'。
req = requests.get(url)
soup = BeautifulSoup(req.content, 'html.parser')
標題 = [i.text.replace('
', '') for i in soup.find_all('div', class_="title") ]
做到了你所要的。
uj5u.com熱心網友回復:
你可以這樣做。
- 歌曲標題存在于
<div>標簽內,類名為title。 - 用
.find_all()選擇所有這些<div>。這將給你一個所有<div>標簽的串列。 - 遍歷該串列并列印每個div的文本。
from bs4 import BeautifulSoup
import requests
url = 'https://www.officialcharts.com/charts/singles-chart/'/span>
r = requests.get(url)
soup = BeautifulSoup(r.text, 'lxml')
d = soup.find_all('div'/span>, class_='title')
for i in d。
print(i.text.strip() )
輸出示例:
不良習慣
保持
銘記在心
黑魔法
來訪時間
比以前更快樂
工業寶貝
浪費了
.
.
.
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/314872.html
標籤:
