无法从此元素获取链接:
<h3 class="proposition_name">
<a href="/newauto/auto-jeep-grand-cherokee-1834871.html">
<strong>Jeep Grand Cherokee 2019</strong>
</a>
</h3>
这是我的代码:
import requests
from bs4 import BeautifulSoup
URL = 'https://auto.ria.com/newauto/marka-jeep/'
HEADERS={'user-agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36','accept':'*/*'}
def get_html(url,params=None):
r = requests.get(url,headers=HEADERS,params=params)
return r
def get_content(html):
soup = BeautifulSoup(html,'html.parser')
itemsdiv = soup.findAll('div', class_='proposition') #class that contains the upper element
cars = []
for itemdiv in itemsdiv:
cars.append({
'title': itemdiv.find('h3',class_='proposition_name').get_text(strip=True),
})
print(itemdiv.find('a',href_='proposition_area').get_text())#here i am trying to get the link
print(cars)
def parse():
html = get_html(URL)
if html.status_code==200:
get_content(html.text)
else:
print('Error')
parse()
我试过的
1)print(itemdiv.find('a',href_='proposition_area').get_text())# gettin none
2)创建另一个带有参数“ a”的项目
itemsa = soup.findAll('a', class_='proposition')
然后另一个for循环
for itema in itemsa:
print(itema.get('href'))
3)将每个项目打印为文本
for itemdiv in itemsdiv:
cars.append({
'title': itemdiv.find('h3',class_='proposition_name').get_text(strip=True),
})
print(itemdiv.get_text())
但是里面没有链接