I am trying to do web scraping using python. When i try to create a data frame to store my variable with extracted information, it shows "ValueError: If using all scalar values, you must pass an index". I already check other related post in this website by trying to indexing {'trade_name':trade_name}, index=[0])
, but still unable to solve. Please help.
import pandas as pd
import requests
import urllib.request
import time
from bs4 import BeautifulSoup
url = 'https://www.medindia.net/doctors/drug_information/abacavir.htm'
response = requests.get(url)
soup = BeautifulSoup(response.text, "html.parser")
drug = soup.find(class_='mi-container__fluid')
print(drug)
# whole page contain drug content
items = drug.find_all(class_='report-content drug-widget')
print(items)
# extract drug information from drug content into individual variable
trade_name = print(items[0].find(class_='drug-content').get_text())
function = print(items[1].find(class_='drug-content').get_text())
Contraindications = print(items[2].find(class_='drug-content').get_text())
Dosage = print(items[3].find(class_='drug-content').get_text())
how_to_use = print(items[4].find(class_='drug-content').get_text())
warnings = print(items[5].find(class_='drug-content').get_text())
storage = print(items[7].find(class_='drug-content').get_text())
drug_stuff = pd.DataFrame(
{
'trade_name':trade_name,
'function': function,
'Contraindications': Contraindications,
'Dosage': Dosage,
'how_to_use':how_to_use,
'warnings':warnings,
'storage':storage,
})
print(drug_stuff)
print()
always returnNone
- sotrade_name = print( ...)
works liketrade_name = None
. Removeprint()
to assign value to variablatrade_name = items[0].find(class_='drug-content').get_text()
– furasDataFrame
you have to use list with elments - even if you have only one element -'trade_name': [trade_name], ..., 'storage': [storage],
- and then it works without warning – furasget_text(strip=True)
. Eventuallytext = text.replace("\n", "")
– furas