在维基上使用;tr";及;运输署";使用BeautifulSoup和python

在维基上使用;tr";及;运输署";使用BeautifulSoup和python,python,beautifulsoup,wiki,Python,Beautifulsoup,Wiki,总蟒蛇3初学者在这里。我似乎无法打印出学院的名称。 这个班离学校的名字不远,我似乎无法把我需要的缩小范围。并打印到新的csv文件。有什么想法吗 import requests from bs4 import BeautifulSoup import csv res= requests.get("https://en.wikipedia.org/wiki/Ivy_League") soup = BeautifulSoup(res.text, "html.parser") colleges =

总蟒蛇3初学者在这里。我似乎无法打印出学院的名称。 这个班离学校的名字不远,我似乎无法把我需要的缩小范围。并打印到新的csv文件。有什么想法吗

import requests
from bs4 import BeautifulSoup
import csv


res= requests.get("https://en.wikipedia.org/wiki/Ivy_League")
soup = BeautifulSoup(res.text, "html.parser")
colleges = soup.find_all("table", class_ = "wikitable sortable")

for college in colleges:
    first_level = college.find_all("tr")
    print(first_level)
与:

你得到了这个班级所有的表(有五个),而不是表中所有的大学。所以你可以这样做:

import requests
from bs4 import BeautifulSoup

res= requests.get("https://en.wikipedia.org/wiki/Ivy_League")
soup = BeautifulSoup(res.text, "html.parser")

college_table = soup.find("table", class_ = "wikitable sortable")
colleges = college_table.find_all("tr")

for college in colleges:
    college_row = college.find('td')
    college_link = college.find('a')
    if college_link != None:
        college_name = college_link.text
        print(college_name)

EDIT:我添加了一个if以丢弃第一行,该行有表头

您可以使用
soup.select()
来使用css选择器,更精确地说:

import requests
from bs4 import BeautifulSoup

res= requests.get("https://en.wikipedia.org/wiki/Ivy_League")
soup = BeautifulSoup(res.text, "html.parser")

l = soup.select(".mw-parser-output > table:nth-of-type(2) > tbody > tr > td:nth-of-type(1) a")
for each in l:
    print(each.text)
打印结果:

Brown University
Columbia University
Cornell University
Dartmouth College
Harvard University
University of Pennsylvania
Princeton University
Yale University
要将单个列放入csv,请执行以下操作:

import pandas as pd
pd.DataFrame([e.text for e in l]).to_csv("your_csv.csv") # This will include index

那…好吧…太美了!太神了我必须在第n个类型上查找css规则,以了解如何准确地使用它。您的解决方案非常有效!关于如何打印到csv有什么想法吗?@DelleIsDelle如果您想使用单列csv,只需在每行末尾添加
。或者您可以使用添加到答案中的熊猫。关于如何打印到csv,您有什么想法吗?这是另一个问题,但您可以从中获得想法:
import pandas as pd
pd.DataFrame([e.text for e in l]).to_csv("your_csv.csv") # This will include index