第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

HTML 中 div 元素上的 Beautiful Soup 循環(huán)

HTML 中 div 元素上的 Beautiful Soup 循環(huán)

繁星coding 2023-09-18 17:21:15
我正在嘗試使用 Beautiful Soup 從網(wǎng)頁(yè)中提取一些值(這里不是很聰明..),這些值是來(lái)自 Weatherbug 預(yù)報(bào)的每小時(shí)值。在 Chrome 開(kāi)發(fā)者模式下,我可以看到這些值嵌套在div類中,如下面的片段所示:在 Python 中,我可以嘗試模仿 Web 瀏覽器并找到這些值:import requestsimport bs4 as BeautifulSoupimport pandas as pdfrom bs4 import BeautifulSoupurl = 'https://www.weatherbug.com/weather-forecast/hourly/san-francisco-ca-94103'header = {  "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.75 Safari/537.36",  "X-Requested-With": "XMLHttpRequest"}page = requests.get(url, headers=header)soup = BeautifulSoup(page.text, 'html.parser')通過(guò)下面的代碼,我可以找到 12 個(gè)這樣的hour-card_mobile_conddiv 類,這似乎是正確的,因?yàn)樵谒阉髅啃r(shí)預(yù)測(cè)時(shí),我可以看到未來(lái)數(shù)據(jù)的 12 小時(shí)/變量。我不確定為什么我要選擇移動(dòng)設(shè)備方法來(lái)查看...(?)temp_containers = soup.find_all('div', class_ = 'hour-card__mobile__cond')print(type(temp_containers))print(len(temp_containers))輸出:<class 'bs4.element.ResultSet'>12如果我嘗試編寫一些代碼來(lái)循環(huán)遍歷所有這些 div 類以進(jìn)一步深入,我會(huì)在下面做一些不正確的事情。我可以返回 12 個(gè)空列表。有人能給我一些可以改進(jìn)的提示嗎?最終,我希望將所有 12 個(gè)未來(lái)每小時(shí)預(yù)測(cè)值放入 pandas 數(shù)據(jù)框中。for div in temp_containers:    a = div.find_all('div', class_ = 'temp ng-binding')    print(a)編輯,基于 pandas 數(shù)據(jù)框答案的完整代碼import requestsfrom bs4 import BeautifulSoupimport pandas as pdr = requests.get(    "https://www.weatherbug.com/weather-forecast/hourly/san-francisco-ca-94103")soup = BeautifulSoup(r.text, 'html.parser')stuff = []for item in soup.select("div.hour-card__mobile__cond"):    item = int(item.contents[1].get_text(strip=True)[:-1])    print(item)    stuff.append(item)df = pd.DataFrame(stuff)df.columns = ['temp']
查看完整描述

2 回答

?
夢(mèng)里花落0921

TA貢獻(xiàn)1772條經(jīng)驗(yàn) 獲得超6個(gè)贊

頁(yè)面加載后,網(wǎng)站就會(huì)動(dòng)態(tài)加載JavaScript。所以你可以使用requests-html或selenium.

from selenium import webdriver

from selenium.webdriver.firefox.options import Options


options = Options()

options.add_argument('--headless')

driver = webdriver.Firefox(options=options)


driver.get(

? ? "https://www.weatherbug.com/weather-forecast/hourly/san-francisco-ca-94103")



data = driver.find_elements_by_css_selector("div.temp.ng-binding")


for item in data:

? ? print(item.text)


driver.quit()

輸出:


51°


52°


53°


54°


53°


53°


52°


51°


51°


50°


50°


49°

根據(jù)用戶請(qǐng)求更新:


import requests

from bs4 import BeautifulSoup


r = requests.get(

? ? "https://www.weatherbug.com/weather-forecast/hourly/san-francisco-ca-94103")

soup = BeautifulSoup(r.text, 'html.parser')


for item in soup.select("div.hour-card__mobile__cond"):

? ? item = int(item.contents[1].get_text(strip=True)[:-1])

? ? print(item, type(item))

輸出:


51 <class 'int'>

52 <class 'int'>

53 <class 'int'>

53 <class 'int'>

53 <class 'int'>

53 <class 'int'>

52 <class 'int'>

51 <class 'int'>

51 <class 'int'>

50 <class 'int'>

50 <class 'int'>

50 <class 'int'>


查看完整回答
反對(duì) 回復(fù) 2023-09-18
?
千萬(wàn)里不及你

TA貢獻(xiàn)1784條經(jīng)驗(yàn) 獲得超9個(gè)贊

當(dāng)您看到 class = "temp ng-binding" 時(shí),這意味著該 div 具有“temp”類和“ng-binding”類,因此查找兩者都不起作用。另外,當(dāng)我運(yùn)行你的腳本時(shí),臨時(shí)容器的 html 看起來(lái)像這樣:


print(temp_containers[0])


<div class="temp">

                    51°

</div>

所以我運(yùn)行了這個(gè)并得到了結(jié)果


import requests

import pandas as pd

from bs4 import BeautifulSoup


url = 'https://www.weatherbug.com/weather-forecast/hourly/san-francisco-ca-94103'


header = {

  "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.75 Safari/537.36",

  "X-Requested-With": "XMLHttpRequest"

}


page = requests.get(url, headers=header)


soup = BeautifulSoup(page.text, 'html.parser')


temp_containers = soup.find_all('div', class_ = 'hour-card__mobile__cond')

print(type(temp_containers))

print(len(temp_containers))


for div in temp_containers:

    a = div.find('div', class_ = 'temp')

    print(a.text)


查看完整回答
反對(duì) 回復(fù) 2023-09-18
  • 2 回答
  • 0 關(guān)注
  • 138 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)