如何从搜索结果中提取链接的href?

时间:2020-09-06 19:30:13

标签: beautifulsoup

我尝试了以下引用,但无法获取结果链接...

import requests
from requests import get
from bs4 import BeautifulSoup

url = "https://www.immoweb.be/en/search/house-and-apartment/for-sale?countries=BE&page=1&orderBy=relevance"

results = requests.get(url)

soup = BeautifulSoup(results.text, "html.parser")

links =[]

for el in soup.find_all('li', {'class': 'card__title-link'}):

    links.append(el.find('a').get('href'))

links
[]

1 个答案:

答案 0 :(得分:1)

根据页面内的Json数据动态构建链接。要打印它们,您可以执行以下操作:

Option Explicit ' Consider this mandatory
' Tools | Options | Editor tab
' Require Variable Declaration
' If desperate declare as Variant

Sub FlagRequest_multiple_values()
    
    Dim oItems As Items
    Dim oItemsRes As Items
        
    Dim Category_Filter As String
    Dim Category_Flag_Filter As String
    
    Dim EventCode As String
    
    Dim i As Long
    
    Dim resItemsCount As Long
    Dim matchCount As Long
    
    Set oItems = Session.GetDefaultFolder(olFolderInbox).Folders("FlagRequestTest").Items
    
    EventCode = "AI-G167"
    'EventCode = "HR-T245"
    Debug.Print
    Debug.Print "EventCode......: " & EventCode
    
    ' with single quotes added to variable
    Category_Flag_Filter = _
      "[Categories] = 'Seminars' And [FlagRequest] = " & "'" & EventCode & "'"
    Debug.Print
    Debug.Print "Category_Flag_Filter......: " & Category_Flag_Filter
    
    Set oItemsRes = oItems.Restrict(Category_Flag_Filter)
    Debug.Print "Category_Flag_Filter count: " & oItemsRes.Count
    
    Category_Filter = "[Categories] = 'Seminars'"
    Debug.Print
    Debug.Print "Category_Filter......: " & Category_Filter

    Set oItemsRes = oItems.Restrict(Category_Filter)
    Debug.Print "Category_Filter count: " & oItemsRes.Count
    
    If oItemsRes.Count > 0 Then
        
        resItemsCount = oItemsRes.Count
        
        For i = 1 To resItemsCount
            With oItemsRes(i)
            
                Debug.Print i & " Subject......: " & .Subject
                Debug.Print "    FlagRequest: " & .FlagRequest

                If InStr(.FlagRequest, EventCode) > 0 Then
                    matchCount = matchCount + 1
                Else
                    Debug.Print "    *** No match ***"
                End If
                
            End With
        Next
        
    End If
    
    Debug.Print matchCount & " matches of " & resItemsCount
    
End Sub

打印:

import json
import requests
from bs4 import BeautifulSoup


url = 'https://www.immoweb.be/en/search/house-and-apartment/for-sale?countries=BE&page=1&orderBy=relevance'
soup = BeautifulSoup(requests.get(url).content, 'html.parser')

data = json.loads( soup.find('iw-search')[':results'] )

# uncomment this to print all data:
# print(json.dumps(data, indent=4))

for d in data:
    print(d['property']['title'])
    print('https://www.immoweb.be/en/classified/{}'.format(d['id']))
    print('-' * 80)