python web scraping(如果使用所有标量值,则必须传递索引)

时间:2019-11-23 14:01:03

标签: python indexing scalar

我正在尝试使用python进行网页抓取。当我尝试创建一个数据框来存储带有提取信息的变量时,它显示“ ValueError:如果使用所有标量值,则必须传递索引”。我已经尝试通过索引{'trade_name':trade_name}, index=[0])来检查此网站上的其他相关帖子,但仍然无法解决。请帮忙。

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)

1 个答案:

答案 0 :(得分:0)

首先:print()始终返回None-因此trade_name = print(...)的工作方式类似于trade_name = None,但您什么也没得到。删除print()为variabla赋值

 trade_name = items[0].find(class_='drug-content').get_text()

要创建DataFrame,您必须使用带有元素的列表-即使您只有一个元素-

'trade_name': [trade_name], ..., 'storage': [storage],

然后可以正常工作而不会发出警告。

如果不使用[],则它将trade_namestorage中的字符串视为字符列表,并将每个字符放在单独的行中。由于字符串具有不同数量的字符,因此它们创建的行数也不同,这可能会导致问题并显示警告。


顺便说一句:您可以使用get_text(strip=True)删除一些无用的空格,制表符和文本输入。


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 = items[0].find(class_='drug-content').get_text(strip=True)
function = items[1].find(class_='drug-content').get_text(strip=True)
contraindications = items[2].find(class_='drug-content').get_text(strip=True)
dosage = items[3].find(class_='drug-content').get_text(strip=True)
how_to_use = items[4].find(class_='drug-content').get_text(strip=True)
warnings = items[5].find(class_='drug-content').get_text(strip=True)
storage = items[7].find(class_='drug-content').get_text(strip=True)


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)