亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

NameError:未定義名稱“項目”

NameError:未定義名稱“項目”

皈依舞 2023-04-25 16:24:04
采納了建議,我能夠通過最初的錯誤,到目前為止非常感謝你們 :) 我快到了我想去的地方。似乎在縮進方面我仍然存在巨大的知識差距。你們真的是編碼社區的瑰寶,到目前為止非常感謝你們:)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')我得到的錯誤是以下我使用 pycharm 2020.2 也許它是一個糟糕的選擇?row = [listing_title, listing_url, listing_image_url, property_location, property_type, property_status, property_size] NameError: name 'property_location' 未定義
查看完整描述

4 回答

?
尚方寶劍之說

TA貢獻1788條經驗 獲得超4個贊

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

只需將 for 循環及其內容放在迭代 pipeline_items 中的項目的 for 循環中。


查看完整回答
反對 回復 2023-04-25
?
慕容3067478

TA貢獻1773條經驗 獲得超3個贊

問題是

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

返回一個空列表。這樣做的結果是:

for item in pipeline_items:

從來沒有真正發生過。因此,item永遠不會定義 的值。

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

  1. 縮進for p_tag in item.find_all('p'):以便為每個項目執行它。這樣,如果沒有項目,它就不會被調用(我想這就是你原本打算做的?)

  2. 在循環前加if語句判斷是否item存在,不存在則跳過循環。哪個最接近復制您的代碼當前正在執行的操作,但我認為這不是您希望它執行的操作。


查看完整回答
反對 回復 2023-04-25
?
嗶嗶one

TA貢獻1854條經驗 獲得超8個贊

第 17 行及以下需要在 for 循環內才能看到“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:':


查看完整回答
反對 回復 2023-04-25
?
qq_花開花謝_0

TA貢獻1835條經驗 獲得超7個贊

任務完成感謝這里的每一個人,干杯!我遺漏了一些東西。1 確定縮進。2 我錯過了第一小節的跨度——如果 p_tag.span.text == 'Location:': 3 我錯過了一個包 openpyxl,它在底部被調用以寫入 excel。


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


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')


查看完整回答
反對 回復 2023-04-25
  • 4 回答
  • 0 關注
  • 179 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號