【问题标题】:How to scrape information from a website and skip to the next point if the information is not existing如果信息不存在,如何从网站上抓取信息并跳到下一点
【发布时间】:2019-06-20 08:11:17
【问题描述】:

我想从房地产网站上抓取信息以进行市场研究。 我现在的问题是,如果某个房子没有我想要的所有信息(例如,房地产经纪人没有在他的曝光中放置房子的大小),那么脚本就会停止并且我得到一个错误。

我已经用if/else 语句尝试过,但我犯了一些错误,所以它不能像这样工作。 我得到错误:

"UnboundLocalError: 之前引用的局部变量 'Flattyp' 任务”

def get_house_info (House):
    for id in ids:
            sourceCode = urllib.request.urlopen('https://www.immobilienscout24.de/expose/' + str(id)).read()
            purchasePrice = str(sourceCode).split('"purchasePrice":')[1].split(',"geoCode"')[0]
            Spacesize = str(sourceCode).split('"area":')[1].split('},"details"')[0]
            District = str(sourceCode).split('"quarter":')[1].split('},')[0]
            if Flattyp != str(sourceCode).split('"is24qa-typ grid-item three-fifths">')[1].split('</dd> </dl> <dl class')[0]:
                Flattyp = "nicht vorhanden"
            else:
                Flattyp = str(sourceCode).split('"is24qa-typ grid-item three-fifths">')[1].split('</dd> </dl> <dl class')[0]
            Rooms = str(sourceCode).split('is24qa-zimmer grid-item three-fifths"> ')[1].split(' </dd> </dl> <dl class=')[0]
            parking_space = str(sourceCode).split('<dd class="is24qa-garage-stellplatz grid-item three-fifths">')[1].split('</dd> </dl>')[0]
            if parking_space != str(sourceCode).split('<dd class="is24qa-garage-stellplatz grid-item three-fifths">')[1].split('</dd> </dl>')[0]:
                parking_space = "nicht vorhanden"
            else: 
                parking_space = str(sourceCode).split('<dd class="is24qa-garage-stellplatz grid-item three-fifths">')[1].split('</dd> </dl>')[0]

            with open('fooneu23.txt', 'a') as csvfile:
                cols = ['id', 'price', 'size', 'district', 'flattyp', 'rooms', 'parking_space','Flattypp']
                dict_result = {'id': id, 'price': purchasePrice, 'size': Spacesize, 'district': District, 'flattyp': Flattyp, 'rooms': Rooms, 'parking_space':parking_space, 'Flattypp':Flattypp}
                writer = csv.DictWriter(csvfile, fieldnames=cols)
                writer.writeheader()
                writer.writerow(dict_result)

            csvfile.close()

【问题讨论】:

    标签: python if-statement web-scraping


    【解决方案1】:

    错误在这里:

    if Flattyp != str(sourceCode).split('"is24qa-typ grid-item three-fifths">')[1].split('</dd> </dl> <dl class')[0]:
        Flattyp = "nicht vorhanden"
    else:
        Flattyp = str(sourceCode).split('"is24qa-typ grid-item three-fifths">')[1].split('</dd> </dl> <dl class')[0]
    

    在第一次迭代中,您将Flattyp 与某物进行比较,而Flattyp 尚未设置。

    对于您最初的问题,您听说过try/catch 声明吗?

    您应该一劳永逸地解码您请求的所有响应,而不是多次将其转换为字符串:

    sourceCode = urllib.request.urlopen(
                      'https://www.immobilienscout24.de/expose/' + str(id)
                 ).read().decode('utf8')
    

    最后,你不应该使用 str 函数(regexp、split)来解析 html。使用适当的 html 解析器,例如 beautifulsoup

    【讨论】:

    • 我尝试了 try/except 语句,但使用这些语句,脚本会跳过整个曝光,而不是只跳过一个缺失的信息。
    • @CosmoCramer 无论如何尝试使用 beautifulsoup 和正确的 css 选择器,您可以使用空字符串而不是错误,这将更方便您解决问题。
    猜你喜欢
    • 2021-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-09
    • 1970-01-01
    相关资源
    最近更新 更多