【问题标题】:Web Scrape: How to get all name and price from all this link?Web Scrape:如何从所有这些链接中获取所有名称和价格?
【发布时间】:2019-12-01 09:44:48
【问题描述】:

你好朋友,我正在尝试获取所有数据,例如名称价格和来自此所有链接的其他数据我在 python 中获取所有 href 链接,但我不知道如何立即获取我正在尝试的所有名称和价格获取但我收到错误我只能打印此代码中的所有链接,但我需要此链接中的所有数据(名称、价格、另一个),请帮助我如何做到这一点,这是我的代码。

url='https://m.autocentrum.pl/nowe/'
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')

# To download the whole data set, let's do a for loop through all a tags
for i in range(80,len(soup.findAll('a'))+1): #'a' tags are for links
    one_a_tag = soup.findAll('a')[i]
    link = one_a_tag['href']
    download_url = 'http://m.autocentrum.pl'+ link

#    urllib.request.urlretrieve(download_url,+link[link.find("div", class_="car-offer")+1:]) 



#     for i in range(len(soup1.findall("div"))):
#         name = download_url.find("div", class_="new-car-header")[i]
#         print(name)
#     page = requests.get(download_url)
#     soup = BeautifulSoup(page.text, 'html.parser')

#     rating = soup.find_all("div", class_="car-offer") # this is the main div inside this div all data it is so how to export in csv file

#     last_links = soup.find(class_='car-offer')
#     last_links.decompose()
    #name = soup.find(class_='new-car-header')
#     last_links = download_url.find(class_='car-offer')
#     last_links.decompose()
#     name = download_url.find(class_='new-car-header')
#     price = download_url.find(class_='mobile-car-price')
#     extra = more-info
#     artist_name_list_items = artist_name_list.find_all('a')

#urllib.request.urlretrieve(download_url,'./'+link[link.find('/turnstile_')+1:]) 
    print(download_url)
    time.sleep(1) #pause the code for a sec

【问题讨论】:

    标签: html python-3.x web-scraping


    【解决方案1】:

    一种可能性是使用 CSS 选择器选择正确的元素并使用 zip() 函数将它们绑定在一起:

    import requests
    from bs4 import BeautifulSoup
    
    soup = BeautifulSoup(requests.get('https://www.autocentrum.pl/nowe/').text, 'html.parser')
    
    print('{:<20}{}'.format('Name', 'Price'))
    print('-' * 30)
    for name, price in zip(soup.select('.car-offer h2'), soup.select('.car-offer div.price .number')):
        name, price = name.get_text(strip=True), price.get_text(strip=True, separator=' ')
        print('{:<20}{}'.format(name, price))
    

    打印:

    Name                Price
    ------------------------------
    Opel Astra          85 200 PLN
    Fiat Doblo          84 800 PLN
    Nissan NV200        57 310 PLN
    

    【讨论】:

    • 为什么我只得到 3 个数据而不是更多 @AndrejKesely
    • @rahulraj 当我访问autocentrum.pl/nowe 时,我看到那里只有 3 辆车,所以这就是为什么只有 3 个结果
    • 下面是所有汽车品牌名称,我需要所有汽车列表以及所有品牌汽车的名称和价格@AndrejKesely 请帮我兄弟我需要这样做
    • @rahulraj 我只能说你必须获取每个汽车品牌的所有链接,为每个品牌提出新请求并在那里刮掉汽车信息。
    • 我不能一下子做是不是不可能? @AndrejKesely
    猜你喜欢
    • 2018-09-04
    • 2023-03-14
    • 2016-01-26
    • 1970-01-01
    • 2015-06-15
    • 2022-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多