Python-将两个for循环连接到数组中

问题描述 投票:0回答:1

我是python的新手,可以随时学习,试图结合来自多个教程的知识来解决我的问题。

本质上,我正在寻找以下网站,以将所有属性及其对应的页面链接提取到一个数组中。

网站:“ https://www.accommodationforstudents.com/search-results?location=London&area=&beds=0&searchType=halls&price=undefined&limit=99

问题是,当我运行代码时,它会正确地循环访问每个属性的链接,但是属性名称却没有。我将不胜感激。

问候

.....

import urllib.request
import requests
from bs4 import BeautifulSoup

url = "https://www.accommodationforstudents.com/search-results?location=London&area=&beds=0&searchType=halls&price=undefined&limit=99"

response = requests.get(url)
soup = BeautifulSoup(response.text, "html.parser")

page = soup.findAll('a', attrs={'class': "student-halls-card__link"})
property = soup.findAll('strong', attrs={'class': "student-halls-card__title"})

sites = []


for link in page:
    link.find('href', attrs={'class': "student-halls-card__link"})

    for name in property:
        name.find('href', attrs={'class': 'student-halls-card__title'})

    sites.append(name.text + " - " + "https://www.accommodationforstudents.com" + link.get('href'))

print(sites)

........

结果缩短。'Felda House-https://www.accommodationforstudents.com/student-hall/407','Felda House-https://www.accommodationforstudents.com/student-hall/1672','Felda House-https://www.accommodationforstudents.com/student-hall/3260',,................]

python arrays append concatenation screen-scraping
1个回答
0
投票

尝试一下。

for link, name in zip(page, property):
© www.soinside.com 2019 - 2024. All rights reserved.