如何遍历列表网络抓取的表格列并为每个项目返回一个结果?

时间:2019-02-17 13:55:15

标签: python web-scraping

我有一个python代码,可在网络上抓取正确的数据,但guest虚拟机列中包含多个字符串,并且目前仅可读取其中一个。那么,如何遍历该列单元格中的列表,并将3个guest作为单独的列返回给希望的guest1,guest2,guest3? 谢谢

import requests
import pandas as pd
from bs4 import BeautifulSoup
import numpy as np

df = pd.DataFrame(columns=(['NoInSeason', 'Guests', 'Winner', 'OriginalAirDate']))
page = requests.get("https://en.wikipedia.org/wiki/List_of_QI_episodes")
soup = BeautifulSoup(page.content, "lxml")
my_tables = soup.find_all("table",{"class":"wikitable plainrowheaders wikiepisodetable"})
for table in my_tables:
    table_rows = table.find_all("tr")
    for tr in table_rows:
        td = tr.find_all("td")
        if len(td) == 5:
            NoInSeason = td[0].find(text=True)
            Guests = td[2].find_all(text=True)
            Winner  = td[3].find(text=True)
            OriginalAirDate = td[4].find(text=True) 
            if len(Guests) == 3:
                Guest1 = Guests[0]
                Guest2 = Guests[1]
                Guest3 = Guests[2]
                df = df.append({'NoInSeason': NoInSeason, 'Guest1' : Guest1, 'Guest2' : Guest2, 'Guest3' : Guest3, 'Winner': Winner, 'OriginalAirDate' : OriginalAirDate}, ignore_index=True)
df.to_csv("output.csv")
print(df)

1 个答案:

答案 0 :(得分:1)

这是您要找的吗?

df = pd.DataFrame(columns=(['NoInSeason', 'Guest 1', 
'Guest 2', 'Guest 3', 'Winner', 'OriginalAirDate']))
page = 
  requests.get("https://en.wikipedia.org/wiki/List_of_QI_episodes")
soup = BeautifulSoup(page.content, "lxml")
my_tables = soup.find_all("table",{"class":"wikitable plainrowheaders wikiepisodetable"})
for table in my_tables:
    table_rows = table.find_all("tr")
    for tr in table_rows:
        td = tr.find_all("td")
        if len(td) == 5:
            NoInSeason = td[0].find(text=True)
            Guests = td[2].find_all(text=True)
            Winner  = td[3].find(text=True)
            OriginalAirDate = td[4].find(text=True)
            print(Guests)
            try:
                df = df.append({'NoInSeason': NoInSeason, 'Guest 1' : Guests[0], 'Guest 2' : Guests[1], 'Guest 3' : Guests[2], 'Winner': Winner, 'OriginalAirDate' : OriginalAirDate}, ignore_index=True)
            except IndexError as index_error:
                continue
print(df)

编辑:我看到您更改了代码,现在可以了吗?并且在DataFrame中包含Guest1,Guest2和Guest3列是否会更好,这样您就不会得到充满NaN的“ Guests”列?