Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用find Beauty soup使用python获取href_Python_Web Scraping_Beautifulsoup_Location Href - Fatal编程技术网

使用find Beauty soup使用python获取href

使用find Beauty soup使用python获取href,python,web-scraping,beautifulsoup,location-href,Python,Web Scraping,Beautifulsoup,Location Href,我正试图抓取一个房地产网站,但不知道如何将URL链接到每个房产 以下是我现在掌握的代码: 我对编码还是新手,我搜索了其他类似的主题,但找不到一个完全符合我问题的答案 作为pd进口熊猫 从selenium导入webdriver 从selenium.webdriver.chrome.options导入选项 从bs4导入BeautifulSoup 选项=选项() 选项。添加参数(“窗口大小=1400600”) 从fake_useragent导入useragent ua=UserAgent() a=u

我正试图抓取一个房地产网站,但不知道如何将URL链接到每个房产

以下是我现在掌握的代码:

我对编码还是新手,我搜索了其他类似的主题,但找不到一个完全符合我问题的答案


作为pd进口熊猫
从selenium导入webdriver
从selenium.webdriver.chrome.options导入选项
从bs4导入BeautifulSoup
选项=选项()
选项。添加参数(“窗口大小=1400600”)
从fake_useragent导入useragent
ua=UserAgent()
a=ua.random
用户\代理=ua.random
打印(用户/代理)
options.add_参数(f'user-agent={user_-agent}')
driver=webdriver.Chrome('/Users/raduulea/Documents/chromedriver',options=options)
司机,上车https://www.immoweb.be/fr/recherche/appartement/a-vendre')
导入时间
时间。睡眠(10)
标题=[]
地址=[]
价格=[]
表面=[]
desc=[]
页码=2
尽管如此:
时间。睡眠(10)
html=driver.page\u源
soup=BeautifulSoup(html,'html.parser')
如果int(第页)>1:
results=soup.find_all(True,{“class”:[“result xl”、“result-l”、“result-m”]})
对于结果中的结果:
Title.append(result.find(“div”,“class”:“Title bar left”}).get_text().strip())
address.append(result.find(“span”,{“result address”}).get_text().strip())
price.append(result.find(“div”,“class”:[“xl价格区间价格”,“l价格区间价格”,“m价格区间价格”,“xl价格促销区间价格”]})。get_text().strip())
append(result.find(“div”,“class”:[“xl surface ch”,“l-surface-ch”,“m-surface-ch”]}).get_text().strip())
desc.append(result.find(“div”,“class”:[“xl desc”,“l-desc”,“m-desc”]}).get_text().strip())
如果len(driver.find_elements_by_css_selector(“a.next”))>0:
url=”https://www.immoweb.be/fr/recherche/appartement/a-vendre/?page={}.格式(第页)
获取驱动程序(url)
页码+=1
其他:
打破
df=pd.DataFrame({“Title”:Title,“Address”:Address,“Price:”:Price,“Surface”:Surface,“Description”:desc})
df.to_csv(“immo_a.csv”)
以下是我可以从中获取链接的Html:



提前感谢您的帮助!:)

如果我正确理解下面的问题,代码片段可能会对您有所帮助。我用的是Beautifulsoup。这里ifiWebu\u MAIN是公共属性

from bs4 import BeautifulSoup
data  = ''''<a href="https://www.immoweb.be/fr/annonce/immeuble-a-appartements/a-vendre/hoboken/2660/id8135041" title="Immeuble à appartements de 2 façades à vendre à 2660 Hoboken au prix de 545.000 € - (8135041)" target="IWEB_MAIN" xpath="1"></a>'''
soup = BeautifulSoup(data)
for links in soup.find_all('a',{'target':'IWEB_MAIN'}):
    href = links.get('href')
    title = links.get('title')
    print(href,title)
从bs4导入美化组
数据=“”“
汤=美汤(数据)
对于soup.find_all('a',{'target':'iWebu_MAIN')中的链接:
href=links.get('href')
title=links.get('title')
打印(href,标题)
试试这个:

from selenium import webdriver
import time
from bs4 import BeautifulSoup
from bs4.element import Tag
import pandas as pd
import traceback
from selenium.webdriver.chrome.options import Options


options = Options()
options.add_argument("window-size=1400,600")
from fake_useragent import UserAgent

ua = UserAgent()
user_agent = ua.random

options.add_argument(f'user-agent={user_agent}')
driver = webdriver.Chrome('/Users/raduulea/Documents/chromedriver', options=options)

driver.get('https://www.immoweb.be/fr/recherche/appartement/a-vendre')

time.sleep(4)

title = []
address = []
price = []
surface = []
desc = []
link = []

page = 2

try:
    
    soup = BeautifulSoup(driver.page_source, 'lxml')
    companies = soup.find("div", {"id": "result"})

    for tag in companies:

        if not isinstance(tag, Tag):
            continue

        _class = tag.get('class')

        if _class is None or "result-xl" not in _class[0]:
            continue

        title.append(tag.find("div", {"class":"title-bar-left"}).get_text().strip())
        address.append(tag.find("span", {"result-adress"}).get_text().strip())
        price.append(tag.find("div", {"class": ["xl-price rangePrice", "l-price rangePrice", "m-price rangePrice", "xl-price-promotion rangePrice"]}).get_text().strip())
        surface.append(tag.find("div", {"class": ["xl-surface-ch", "l-surface-ch", "m-surface-ch"]}).get_text().strip())
        desc.append(tag.find("div", {"class": ["xl-desc", "l-desc", "m-desc"]}).get_text().strip())

        links = tag.find("a",href=True)

        link.append(links['href'])

        if len(driver.find_elements_by_css_selector("a.next")) > 0:
            url = "https://www.immoweb.be/fr/recherche/appartement/a-vendre/?page={}".format(page)
            driver.get(url)
            page += 1
        else:
            break

except Exception as e:
    print(e)
    print(traceback.print_exc())

driver.quit()

df = pd.DataFrame({"Title": title, "Address": address, "Price:": price, "Surface": surface, "Description": desc,"Link":link})
df.to_csv("immo_a.csv")
在哪里

刮取每个属性链接

csv文件o/p:

,Title,Address,Price:,Surface,Description,Link
0,Appartement,1090 Jette,"260.000 €  
                                      269.000 €",140 m²     3 ch.,JETTE appartement 2 étages,https://www.immoweb.be/fr/annonce/appartement/a-vendre/jette/1090/id8004072
1,Appartement,6032 Mont-sur-Marchienne,280.000 €,140 m²     4 ch.,Appartement 4 chambres très bien situé,https://www.immoweb.be/fr/annonce/appartement/a-vendre/mont-sur-marchienne/6032/id8137289
2,Appartement,6700 Arlon,210.000 €,110 m²     3 ch.,Appartement spacieux 3 chambres avec garage,https://www.immoweb.be/fr/annonce/appartement/a-vendre/arlon/6700/id8135774
3,Appartement,2000 Anvers,289.000 €,80 m²     1 ch.,Appartement renové avec terrace,https://www.immoweb.be/fr/annonce/appartement/a-vendre/anvers/2000/id8135064
4,Appartement,1200 Woluwe-St-Lambert,"749.000 €  
                                      794.999 €",215 m²     3 ch.,INFOS & VISITE 7/7- SUPERBE DUPLEX PENTHOUSE HOTEL DE MAITRE,https://www.immoweb.be/fr/annonce/appartement/a-vendre/woluwe-st-lambert/1200/id8020453
5,Appartement,9230 Wetteren,199.000 €,95 m²     2 ch.,TOF LICHTRIJK 2-SLKAPPARTEMENT met BALKON in het CENTRUM!,https://www.immoweb.be/fr/annonce/appartement/a-vendre/wetteren/9230/id8134908
6,Duplex,8430 Middelkerke,225.000 €,81 m²     2 ch.,Duplex deux chambres moderne et terrasse,https://www.immoweb.be/fr/annonce/duplex/a-vendre/middelkerke/8430/id8132431
7,Appartement,8400 Ostende,"299.000 €  
                                      320.000 €",80 m²     2 ch.,RESIDENCE OOSTDIJK,https://www.immoweb.be/fr/annonce/appartement/a-vendre/ostende/8400/id6976820
8,Appartement,1000 Bruxelles,450.000 €,121 m²     2 ch.,Appartement ± 135 m² avec terrasse au 4ème étage d'un immeub,https://www.immoweb.be/fr/annonce/appartement/a-vendre/bruxelles/1000/id8132721
9,Penthouse,1180 Uccle,580.000 €,160 m²     2 ch.,Duplex Penthouse avec grande terrasse orientée sud,https://www.immoweb.be/fr/annonce/penthouse/a-vendre/uccle/1180/id8134873
10,Appartement,1050 Ixelles,595.000 €,143 m²     2 ch.,Splendide appartement haut de gamme près du Jardin du Roi,https://www.immoweb.be/fr/annonce/appartement/a-vendre/ixelles/1050/id8134869
11,Appartement,8400 Ostende,"108.000 €  
                                      112.000 €",55 m²     1 ch.,APPARTEMENT DANS UN QUARTIER CALME,https://www.immoweb.be/fr/annonce/appartement/a-vendre/ostende/8400/id8042337
12,Duplex,1180 Uccle,545.000 €,160 m²     4 ch.,Quartier de l'Observatoire DUPLEX-PENTHOUSE 3-4èmes,https://www.immoweb.be/fr/annonce/duplex/a-vendre/uccle/1180/id8131722
13,Appartement,8400 Ostende,185.000 €,75 m²     2 ch.,Appartement,https://www.immoweb.be/fr/annonce/appartement/a-vendre/ostende/8400/id8130087
,Title,Address,Price:,Surface,Description,Link
0,Appartement,1090 Jette,"260.000 €  
                                      269.000 €",140 m²     3 ch.,JETTE appartement 2 étages,https://www.immoweb.be/fr/annonce/appartement/a-vendre/jette/1090/id8004072
1,Appartement,6032 Mont-sur-Marchienne,280.000 €,140 m²     4 ch.,Appartement 4 chambres très bien situé,https://www.immoweb.be/fr/annonce/appartement/a-vendre/mont-sur-marchienne/6032/id8137289
2,Appartement,6700 Arlon,210.000 €,110 m²     3 ch.,Appartement spacieux 3 chambres avec garage,https://www.immoweb.be/fr/annonce/appartement/a-vendre/arlon/6700/id8135774
3,Appartement,2000 Anvers,289.000 €,80 m²     1 ch.,Appartement renové avec terrace,https://www.immoweb.be/fr/annonce/appartement/a-vendre/anvers/2000/id8135064
4,Appartement,1200 Woluwe-St-Lambert,"749.000 €  
                                      794.999 €",215 m²     3 ch.,INFOS & VISITE 7/7- SUPERBE DUPLEX PENTHOUSE HOTEL DE MAITRE,https://www.immoweb.be/fr/annonce/appartement/a-vendre/woluwe-st-lambert/1200/id8020453
5,Appartement,9230 Wetteren,199.000 €,95 m²     2 ch.,TOF LICHTRIJK 2-SLKAPPARTEMENT met BALKON in het CENTRUM!,https://www.immoweb.be/fr/annonce/appartement/a-vendre/wetteren/9230/id8134908
6,Duplex,8430 Middelkerke,225.000 €,81 m²     2 ch.,Duplex deux chambres moderne et terrasse,https://www.immoweb.be/fr/annonce/duplex/a-vendre/middelkerke/8430/id8132431
7,Appartement,8400 Ostende,"299.000 €  
                                      320.000 €",80 m²     2 ch.,RESIDENCE OOSTDIJK,https://www.immoweb.be/fr/annonce/appartement/a-vendre/ostende/8400/id6976820
8,Appartement,1000 Bruxelles,450.000 €,121 m²     2 ch.,Appartement ± 135 m² avec terrasse au 4ème étage d'un immeub,https://www.immoweb.be/fr/annonce/appartement/a-vendre/bruxelles/1000/id8132721
9,Penthouse,1180 Uccle,580.000 €,160 m²     2 ch.,Duplex Penthouse avec grande terrasse orientée sud,https://www.immoweb.be/fr/annonce/penthouse/a-vendre/uccle/1180/id8134873
10,Appartement,1050 Ixelles,595.000 €,143 m²     2 ch.,Splendide appartement haut de gamme près du Jardin du Roi,https://www.immoweb.be/fr/annonce/appartement/a-vendre/ixelles/1050/id8134869
11,Appartement,8400 Ostende,"108.000 €  
                                      112.000 €",55 m²     1 ch.,APPARTEMENT DANS UN QUARTIER CALME,https://www.immoweb.be/fr/annonce/appartement/a-vendre/ostende/8400/id8042337
12,Duplex,1180 Uccle,545.000 €,160 m²     4 ch.,Quartier de l'Observatoire DUPLEX-PENTHOUSE 3-4èmes,https://www.immoweb.be/fr/annonce/duplex/a-vendre/uccle/1180/id8131722
13,Appartement,8400 Ostende,185.000 €,75 m²     2 ch.,Appartement,https://www.immoweb.be/fr/annonce/appartement/a-vendre/ostende/8400/id8130087