繁体   English   中英

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

[英]Python - Concatenate two for loops into array

我是 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/407 ', 'Felda House - https://www.accommodationforstudents.com/studentF-/students72', House https://www.accommodationforstudents.com/student-hall/3260 ', ,........

尝试这个。

for link, name in zip(page, property):

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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