在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問(wèn)答/Python/ 搞的一個(gè)爬蟲(chóng)代碼存在Bug,幫忙看看問(wèn)題出在哪。

搞的一個(gè)爬蟲(chóng)代碼存在Bug,幫忙看看問(wèn)題出在哪。

先放代碼吧

# coding=utf-8
from bs4 import BeautifulSoup
import requests
from time import sleep
#設(shè)置城市
City_Name = 'qd'
page = 'http://newhouse.{0}.fang.com/house/s'.format(City_Name)
#定義Download_Newitem_List()函數(shù)是為了提取指定城市樓盤列表的鏈接,并存放到指定的文件中
def Download_Newitem_List(url,try_num=2):
    global City_Name
    print('正在下載:',url)
    try:
        all_html = requests.get(url,timeout=10)
    except Exception as e:
        print('下載錯(cuò)誤:',e.reason)
        all_html=None
        if try_num >0:
            if hasattr(e,'code') and 500 <= e.code <600:
                return Download_Newitem_List(url,try_num-1)
    all_html.encoding = "gb18030"
    soup = BeautifulSoup(all_html.text, "html5lib")
    #提取新樓盤項(xiàng)目總數(shù)量
    Item_Total = soup.find('a', id="allUrl").find('span').text.replace('(','').replace(')','')
    #如果余數(shù)大于0那么總項(xiàng)目數(shù)整除每頁(yè)20項(xiàng)目數(shù)+1
    if (int(Item_Total) % 20)>0:
        Page_Num = (int(Item_Total) // 20) + 1
    else:
        Page_Num = (int(Item_Total) // 20)
        
    with open('{0}_list_link.txt'.format(City_Name), 'w',encoding='utf-8') as f:
        for i in range(1, Page_Num + 1):
            New_Page_Link = 'http://newhouse.{0}.fang.com/house/s/b9{1}'.format(City_Name, i)
            print(New_Page_Link)
            print(New_Page_Link, file=f)
#定義Download_item_link(City)函數(shù)是為了提取指定城市列表的鏈接中每一個(gè)開(kāi)盤項(xiàng)目的鏈接,并存放到指定的文件中。
def Download_item_link(City):
    with open('{0}_list_link.txt'.format(City), 'r',encoding='utf-8') as f:
        #print(f.readlines())
        for line in f.readlines():
            print('正在讀取:', line)
            sleep(2)
            try:
                all_html = requests.get(line,timeout=10)
                all_html.encoding = "gb18030"
                #print(all_html.text)
            except Exception as e:
                print('下載錯(cuò)誤:', e)
                #if try_num > 0:
                #    if hasattr(e, 'code') and 500 <= e.code < 600:
                #        return Download_Newitem_List(url, try_num - 1)
            soup = BeautifulSoup(all_html.text, "html5lib")
            master_html=soup.find_all('div', class_='nlcd_name')
            with open('{0}_Newall_link.txt'.format(City), 'w',encoding='utf-8') as d:
                for link in master_html:
                    #print(link.get_text().rstrip() + ':' + link.a['href'].rstrip())
                    print(link.a['href'].rstrip(),file=d)

Download_Newitem_List(page)
Download_item_link('qd')

上面這段代碼放到ide中可以直接運(yùn)行
拿青島為例(qd)我提取了總樓盤項(xiàng)目482個(gè),25頁(yè)的頁(yè)面鏈接也都提取出來(lái)了,但是當(dāng)我通過(guò)Download_item_link()這個(gè)函數(shù)提取列表中每個(gè)項(xiàng)目鏈接的時(shí)候就出現(xiàn)問(wèn)題了,按理說(shuō)qd_Newall_link.txt文件中應(yīng)該有482個(gè)鏈接才對(duì),但是怎么弄都是20個(gè)鏈接。琢磨了半天都沒(méi)有搞清楚問(wèn)題出在哪里。
希望哪位大神幫忙看看。

回答
編輯回答
拼未來(lái)

Download_item_link 中的 openw 改成 a


requests.get(line) 改為 requests.get(line.strip())

2018年9月18日 04:17
編輯回答
淚染裳

打開(kāi)文件那里,'w'改為'a'

2017年11月11日 09:01