Advertisement

北京安居客二手房信息爬取

阅读量:

为了实现高效的数据获取需求而引入requests库(数量增加到30%以上)。
从Lxml库中导入etree模块(并将其简写为et)。
# 导入时间库以控制程序执行节奏
从Selenium库中导入WebDriver类(并以简写形式引用)。
导入Pandas数据分析库(并以简写形式引用pd)(数量增加到50%)。

建议将 chromedriver 放置于 Chrome\Application 目录下

chrome_driver = r"C:\Program Files (x86)\Google\Chrome\Application\chromedriver_win32\chromedriver.exe"

chrome_driver = “C:\Program Files (x86)\Google\Chrome\Application\chromedriver_win32\chromedriver.exe”

driver被配置为一个使用webdriver Chrome实例的类实例,并且其executable_path参数设置为指定的路径参数。
request_url等于定义的位置信息链接。

html.render()
html = etree.HTML(html)
#去除空白和换行\n
def format_str(str):
return str.strip().replace('\n', '')

房屋数据集被构造为一个包含属性名称、详细信息、地址和价格的DataFrame对象。
依次遍历从1到3的每个整数i。
构建相应的URL地址。

复制代码
    driver.get(url)
    
    html = driver.find_element_by_xpath("//*").get_attribute("outerHTML")
    
    
    soup = BeautifulSoup(html, "html.parser", from_encoding="utf-8")
    house_list = soup.find_all("li", class_="list-item")
    
    for house in house_list:
    temp = {}
     #提取房源信息
    name = house.find("div", class_="house-title").a.text.strip()
    details = house.find("div", class_="details-item").text.strip()
    address = house.find("span", class_="comm-address").text.strip()
    price = house.find("span", class_="price-det").text.strip()
    # print("name:{} detai:{} address:{} price:{}".format(name, details, address, price))
    
    temp["name"] = format_str(name)
    temp["details"] = format_str(details)
    temp["address"] = format_str(address)
    temp["price"]  = format_str(price)
    houses = houses.append(temp, ignore_index=True)
    
    
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
    
    AI写代码

houses.use_to_csv().generate_csv('beijing.csv', index=False, encoding='utf_8_sig')

全部评论 (0)

还没有任何评论哟~