【问题标题】:Python - Concatenate two for loops into arrayPython - 将两个 for 循环连接到数组中
【发布时间】:2020-02-24 01:25:37
【问题描述】:

我是 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】:

    试试这个。

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

    【讨论】:

    • @reautomation 完美,请随时通过勾选投票按钮下方的复选标记来验证此遮阳篷。
    猜你喜欢
    • 2019-09-11
    • 1970-01-01
    • 1970-01-01
    • 2019-10-30
    • 1970-01-01
    • 2014-01-03
    • 1970-01-01
    • 2021-09-15
    相关资源
    最近更新 更多