Python + BeautifulSoup for循环问题

时间:2018-03-18 20:57:32

标签: python beautifulsoup

我已经坚持了一段时间。有人可以帮忙吗?我将永远感激不尽!

我正在尝试从autotrader中提取广告标题列表,但我的代码无效。 我正在关注this指南。

我的代码如下:

from requests import get
from bs4 import BeautifulSoup
import pandas

url = 'https://www.autotrader.co.uk/car-search?sort=sponsored&sellertype=private'
response = get(url)
html_soup = BeautifulSoup(response.text, 'html.parser')
ad_containers = html_soup.find_all('h2', class_ = 'listing-title title-wrap')
# price_containers = html_soup.find_all('section', class_ = 'price-column')

names = []
# prices = []

for container in ad_containers:
    name = container.find_all('a', class_ ="js-click-handler listing-fpa-link").text
    names.append(name)

#for priceainers in price_containers:
#    price = price_containers.find_all('div', class_ ='vehicle-price').text
#    prices.append(price)

test_df = pandas.DataFrame({'Title': names})
print(test_df.info())
# test_df.to_csv('Autotrader_test.csv')

错误信息如下:

C:\Users\iamcs\venv\untitled42334\Scripts\python.exe 
C:/Users/iamcs/PycharmProjects/untitled42334/hello.py
Traceback (most recent call last):
  File "C:/Users/iamcs/PycharmProjects/untitled42334/hello.py", line 15, in <module>
    name = container.find_all('a', class_ ="js-click-handler listing-fpa-link").text
  File "C:\Users\iamcs\venv\untitled42334\lib\site-packages\bs4\element.py", line 1807, in __getattr__
    "ResultSet object has no attribute '%s'. You're probably treating a list of items like a single item. Did you call find_all() when you meant to call find()?" % key
AttributeError: ResultSet object has no attribute 'text'. You're probably treating a list of items like a single item. Did you call find_all() when you meant to call find()?

Process finished with exit code 1

1 个答案:

答案 0 :(得分:1)

根据消息提示,find_all会返回ResultSet个对象的列表。由于您的container每个只有一个锚标记,因此您需要使用tag.find

names = []
for container in ad_containers:
    name = container.find('a', class_="js-click-handler listing-fpa-link").text
    names.append(name)

或者,

names = [
    c.find('a', class_="js-click-handler listing-fpa-link").text
    for c in ad_containers
]

df = pandas.DataFrame({'Title': names})
df.head()
                                               Title
0                         Nissan Micra 1.2 Visia 5dr
1               Vauxhall Corsa 1.2 i 16v Comfort 3dr
2                       Vauxhall Astra 1.4 Merit 5dr
3                     Suzuki Wagon R 1.3 GL (R+) 5dr
4  Peugeot 106 1.1 INDEPENDENCE 3d 60 BHP MOT 15/...