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

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

NameError:未定義名稱“項(xiàng)目”

NameError:未定義名稱“項(xiàng)目”

皈依舞 2023-04-25 16:24:04
采納了建議,我能夠通過最初的錯(cuò)誤,到目前為止非常感謝你們 :) 我快到了我想去的地方。似乎在縮進(jìn)方面我仍然存在巨大的知識(shí)差距。你們真的是編碼社區(qū)的瑰寶,到目前為止非常感謝你們:)Here is the current code that has passed those errors and its down to a warning, and not extracting anything.import requestsfrom bs4 import BeautifulSoupimport pandas as pdurl = 'https://dc.urbanturf.com/pipeline'response = requests.get(url)soup = BeautifulSoup(response.content, 'html.parser')pipeline_items = soup.find_all('div', attrs={'class': 'pipeline-item'})rows = []columns = ['Listing Title', 'Listing url', 'listing image url', 'location', 'Project type', 'Status', 'Size']for item in pipeline_items:    # title, image url, listing url    listing_title = item.a['title']    listing_url = item.a['href']    listing_image_url = item.a.img['src']    for p_tag in item.find_all('p'):        if not p_tag.h2:            if p_tag.text == 'Location:':                p_tag.span.extract()                property_location = p_tag.text.strip()            elif p_tag.span.text == 'Project type:':                p_tag.span.extract()                property_type = p_tag.text.strip()            elif p_tag.span.text == 'Status:':                p_tag.span.extract()                property_status = p_tag.text.strip()            elif p_tag.span.text == 'Size:':                p_tag.span.extract()                property_size = p_tag.text.strip()      row = [listing_title, listing_url, listing_image_url, property_location, property_type, property_status, property_size]    rows.append(row)    df = pd.Dataframe(rows, columns=columns)    df.to_excel('DC Pipeline Properties.xlsx', index=False)print('File Saved')我得到的錯(cuò)誤是以下我使用 pycharm 2020.2 也許它是一個(gè)糟糕的選擇?row = [listing_title, listing_url, listing_image_url, property_location, property_type, property_status, property_size] NameError: name 'property_location' 未定義
查看完整描述

4 回答

?
尚方寶劍之說

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

在我看來,您的第二個(gè) for 循環(huán)for p_tag in item.find_all('p'):不在第一個(gè) for 循環(huán)的范圍內(nèi),該循環(huán)遍歷項(xiàng)目...添加到第一個(gè)循環(huán)中可能有 0 個(gè)項(xiàng)目的事實(shí),您得到一個(gè)無。

只需將 for 循環(huán)及其內(nèi)容放在迭代 pipeline_items 中的項(xiàng)目的 for 循環(huán)中。


查看完整回答
反對(duì) 回復(fù) 2023-04-25
?
慕容3067478

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

問題是

pipeline_items = soup.find_all('div', attrs={'class': 'pipline-item'})

返回一個(gè)空列表。這樣做的結(jié)果是:

for item in pipeline_items:

從來沒有真正發(fā)生過。因此,item永遠(yuǎn)不會(huì)定義 的值。

我不確定你到底想做什么。但我看到兩個(gè)解決方案:

  1. 縮進(jìn)for p_tag in item.find_all('p'):以便為每個(gè)項(xiàng)目執(zhí)行它。這樣,如果沒有項(xiàng)目,它就不會(huì)被調(diào)用(我想這就是你原本打算做的?)

  2. 在循環(huán)前加if語句判斷是否item存在,不存在則跳過循環(huán)。哪個(gè)最接近復(fù)制您的代碼當(dāng)前正在執(zhí)行的操作,但我認(rèn)為這不是您希望它執(zhí)行的操作。


查看完整回答
反對(duì) 回復(fù) 2023-04-25
?
嗶嗶one

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

第 17 行及以下需要在 for 循環(huán)內(nèi)才能看到“item”。


for item in pipeline_items:

    # title, image url, listing url

        listing_title = item.a['title']

        listing_url = item.a['href']

        listing_image_url = item.a.img['src']

for p_tag in item.find_all('p'):   <------------Indent this for loop to be inside the previous for loop.

    if not p_tag.h2:

        if p_tag.text == 'Location:':


查看完整回答
反對(duì) 回復(fù) 2023-04-25
?
qq_花開花謝_0

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

任務(wù)完成感謝這里的每一個(gè)人,干杯!我遺漏了一些東西。1 確定縮進(jìn)。2 我錯(cuò)過了第一小節(jié)的跨度——如果 p_tag.span.text == 'Location:': 3 我錯(cuò)過了一個(gè)包 openpyxl,它在底部被調(diào)用以寫入 excel。


下面 100% 的工作代碼,我承諾會(huì)變得更好并在我可以的時(shí)候提供幫助 :)


import requests

from bs4 import BeautifulSoup

import pandas as pd


url = 'https://dc.urbanturf.com/pipeline'

response = requests.get(url)

soup = BeautifulSoup(response.content, 'html.parser')


pipeline_items = soup.find_all('div', attrs={'class': 'pipeline-item'})


rows = []

columns = ['listing title', 'listing url', 'listing image url', 'location', 'Project type', 'Status', 'Size']


for item in pipeline_items:

    # title, image url, listing url

    listing_title = item.a['title']

    listing_url = item.a['href']

    listing_image_url = item.a.img['src']


    for p_tag in item.find_all('p'):

        if not p_tag.h2:

            if p_tag.span.text == 'Location:':

                p_tag.span.extract()

                property_location = p_tag.text.strip()

            elif p_tag.span.text == 'Project type:':

                p_tag.span.extract()

                property_type = p_tag.text.strip()

            elif p_tag.span.text == 'Status:':

                p_tag.span.extract()

                property_status = p_tag.text.strip()

            elif p_tag.span.text == 'Size:':

                p_tag.span.extract()

                property_size = p_tag.text.strip()


    row = [listing_title, listing_url, listing_image_url, property_location, property_type, property_status, property_size]

    rows.append(row)

df = pd.DataFrame(rows, columns=columns)

df.to_excel('DC Pipeline Properties.xlsx', index=False)

print('File Saved')


查看完整回答
反對(duì) 回復(fù) 2023-04-25
  • 4 回答
  • 0 關(guān)注
  • 176 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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