简体   繁体   中英

I was trying to do web scraping with Python on Pycharm. Why the csv returned is blank for the following coding? Can anyone try on their own for me?

import time

import pandas as pd
from selenium import webdriver
from selenium.webdriver import Chrome
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager

options = webdriver.ChromeOptions()
options.headless = True

options.page_load_strategy = 'none'

chrome_path = ChromeDriverManager().install()
chrome_service = Service(chrome_path)

driver = Chrome(options=options, service=chrome_service)
driver.implicitly_wait(5)

url= "https://hk.centanet.com/findproperty/list/transaction/%E6%84%89%E6%99%AF%E6%96%B0%E5%9F%8E_3-        DMHSZHHRHD?q=TiDxvVGMUUeutVzA0g1JlQ"

driver.get(url)
time.sleep(10)

contents = driver.find_element(By.CSS_SELECTOR,"div[class*='bx--structured-list-tbody']")

properties = contents.find_elements(By.TAG_NAME,"data-v-af617cf2")

def extract_data(element):
    Date = element.find_elements(By.CSS_SELECTOR, "div[class*='infodate']>span")
    Dev = element.find_elements(By.XPATH, "//*[text() = '愉景新城'")
    Price = element.find_elements(By.CSS_SELECTOR, "div[class*='tranPrice']>span")
    RiseBox = element.find_elements(By.CSS_SELECTOR, "div[class*='riseBox']")
    Area = element.find_elements(By.XPATH, "//*[text() = '呎'")

    return{
        "Date": Date,
        "Development": Dev,
        "Consideration": Price,
        "Change": RiseBox,
        "Area": Area
    }

data = []

for property in properties:
    extracted_data = extract_data(property)
    data.append(extracted_data)

df = pd.DataFrame(data)
df.to_csv("result.csv", index=False)

I was trying to do web scraping on the following link: https://hk.cent.net.com/findproperty/list/transaction/%E6%84%89%E6%99%AF%E6%96%B0%E5%9F%8E_3-DMHSZHHRHD?q=TiDxvVGMUUeutVzA0g1JlQ

and expecting an csv table of data

You don't need the overheads of Selenium to get that data. Here is one way to do it with Requests:

import requests
from bs4 import BeautifulSoup as bs
import pandas as pd

headers = {
    'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36'
}

big_list = []
url = 'https://hk.centanet.com/findproperty/list/transaction/%E6%84%89%E6%99%AF%E6%96%B0%E5%9F%8E_3-DMHSZHHRHD?q=TiDxvVGMUUeutVzA0g1JlQ'

r = requests.get(url, headers=headers)
soup = bs(r.text, 'html.parser')
properties = soup.select('div[class="bx--structured-list-tbody"] div[class="cv-structured-list-item cv-structured-list-item--standard bx--structured-list-row"]')
for p in properties:
    日期 = p.select('div[class="cv-structured-list-data bx--structured-list-td"]')[0].text.strip()
    地址 = p.select('div[class="cv-structured-list-data bx--structured-list-td"]')[1].text.strip()
    平面圖 = p.select('div[class="cv-structured-list-data bx--structured-list-td"]')[2].text.strip()
    成交價 = p.select('div[class="cv-structured-list-data bx--structured-list-td"]')[3].text.strip()
    升跌 = p.select('div[class="cv-structured-list-data bx--structured-list-td"]')[4].text.strip()
    面積 = p.select('div[class="cv-structured-list-data bx--structured-list-td"]')[5].text.strip()
    呎價 = p.select('div[class="cv-structured-list-data bx--structured-list-td"]')[6].text.strip()
    資料來源 = p.select('div[class="cv-structured-list-data bx--structured-list-td"]')[7].text.strip()
    big_list.append((日期, 地址, 平面圖, 成交價, 升跌, 面積, 呎價, 資料來源))
df = pd.DataFrame(big_list, columns = ['日期', '地址', '平面圖', '成交價', '升跌', '面積', '呎價', '資料來源'])
df.to_csv("result.csv", index=False)
print(df)

Result in terminal (also saved as csv):

    日期  地址  平面圖 成交價 升跌  面積  呎價  資料來源
0   2023-01-26  愉景新城 2期 8座 32樓 A室      $850萬   +158%   659呎    @$12,898    土地註冊處
1   2023-01-20  愉景新城 2期 5座 低層 A室       $880萬   --  659呎    @$13,354    中原集團
2   2023-01-20  愉景新城 1期 1座 39樓 B室      $620萬   +199%   474呎    @$13,080    土地註冊處
3   2023-01-20  愉景新城 1期 4座 43樓 C室      $698萬   -1% 474呎    @$14,726    土地註冊處
4   2023-01-20  愉景新城 2期 8座 37樓 D室      $735萬   +126%   600呎    @$12,250    土地註冊處
5   2023-01-19  愉景新城 3期 9座 23樓 F室      $830萬   +29%    650呎    @$12,769    土地註冊處
6   2023-01-18  愉景新城 2期 8座 中層 F室       $17,800 --  648呎    @$27    中原集團
7   2023-01-18  愉景新城 3期 12座 21樓 F室     $870萬   +74%    650呎    @$13,385    土地註冊處
8   2023-01-17  愉景新城 3期 10座 8樓 E室      $698萬   +94%    581呎    @$12,014    土地註冊處
9   2023-01-17  愉景新城 3期 11座 42樓 F室     $640萬   +181%   493呎    @$12,982    土地註冊處
10  2023-01-16  愉景新城 1期 3座 5樓 E室       $744萬   +113%   582呎    @$12,784    土地註冊處
11  2023-01-13  愉景新城 2期 7座 中層 E室       $780萬   --  610呎    @$12,787    中原集團
12  2023-01-13  愉景新城 3期 9座 中層 C室       $678萬   --  477呎    @$14,214    中原集團
13  2023-01-12  愉景新城 1期 2座 7樓 G室       $625萬   +4% 495呎    @$12,626    土地註冊處
14  2023-01-10  愉景新城 1期 3座 高層 F室       $16,000 --  495呎    @$32    中原集團
15  2023-01-10  愉景新城 1期 3座 低層 G室       $13,800 --  495呎    @$28    中原集團
16  2023-01-10  愉景新城 2期 7座 2樓 D室       $700萬   +77%    610呎    @$11,475    土地註冊處
17  2023-01-10  愉景新城 3期 11座 3樓 H室      $810萬   +73%    658呎    @$12,310    土地註冊處
18  2023-01-10  愉景新城 2期 6座 30樓 A室      $838萬   +30%    647呎    @$12,952    土地註冊處
19  2023-01-09  愉景新城 3期 11座 39樓 F室     $645萬   +226%   493呎    @$13,083    土地註冊處
20  2023-01-09  愉景新城 1期 2座 41樓 E室      $712萬   +61%    582呎    @$12,234    土地註冊處
21  2023-01-06  愉景新城 3期 11座 低層 E室      $735萬   --  581呎    @$12,651    中原集團
22  2023-01-06  愉景新城 2期 7座 高層 F室       $645萬   --  485呎    @$13,299    中原集團
23  2023-01-06  愉景新城 2期 7座 中層 G室       $643萬   --  485呎    @$13,258    中原集團

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM