I'm trying to display values from appended list of items that are scraped with bs4. Currently, my code only returns a whole set of data, but I'd like to have separate values displayed from the data. Now, all I get is:
NameError: value is not defined.
How to do it?
data = []
for e in soup.select('div:has(> div > a h3)'):
data.append({
'title':e.h3.text,
'url':e.a.get('href'),
'desc':e.next_sibling.text,
'email': re.search(r'[\w. -] @[\w-] \.[\w.-] ', e.parent.text).group(0) if
re.search(r'[\w. -] @[\w-] \.[\w.-] ', e.parent.text) else None
})
data
title = print(title) # name error
desc = print(desc) # name error
email = print(email) # name error
CodePudding user response:
Main issue is that you try to reference only on keys
without taking into account that there is data
a list of dicts.
So you have to pick your dict by index from data
if you like to print a specific one:
print(data[0]['title'])
print(data[0]['desc'])
print(data[0]['email'])
Alternative just iterate over data
and print/operate on the values of each dict:
for d in data:
print(d['title'])
print(d['desc'])
print(d['email'])
or
for d in data:
title = d['title']
desc = d['desc']
email = d['email']
print(f'print title only: {title}')
CodePudding user response:
You can do that like this way:
for e in soup.select('div:has(> div > a h3)'):
title=e.h3.text,
url=e.a.get('href'),
desc=e.next_sibling.text,
email= re.search(r'[\w. -] @[\w-] \.[\w.-] ', e.parent.text).group(0) if re.search(r'[\w. -] @[\w-] \.[\w.-] ', e.parent.text) else None
print(title)
print(desc)
print(email)
print(url)