美丽的汤:刮表数据

问题描述 投票:1回答:2

我想从下面的URL中提取表数据。具体来说,我想在第一列中提取数据。当我运行下面的代码时,第一列中的数据会重复多次。如何让值只显示在表格中显示一次?

from urllib.request import urlopen
from bs4 import BeautifulSoup
html = urlopen('http://www.pythonscraping.com/pages/page3.html').read()
soup = BeautifulSoup(html, 'lxml')
table = soup.find('table',{'id':'giftList'})

rows = table.find_all('tr')

for row in rows:
    data = row.find_all('td')
    for cell in data:
        print(data[0].text)
python python-3.x web-scraping beautifulsoup python-requests
2个回答
1
投票

试试这个:

from urllib.request import urlopen
from bs4 import BeautifulSoup
html = urlopen('http://www.pythonscraping.com/pages/page3.html').read()
soup = BeautifulSoup(html, 'lxml')
table = soup.find('table',{'id':'giftList'})

rows = table.find_all('tr')

for row in rows:
    data = row.find_all('td')

    if (len(data) > 0):
        cell = data[0]
        print(cell.text)

1
投票

requests模块与selectors结合使用,您也可以尝试以下方法:

import requests
from bs4 import BeautifulSoup

link = 'http://www.pythonscraping.com/pages/page3.html'

soup = BeautifulSoup(requests.get(link).text, 'lxml')
for table in soup.select('table#giftList tr')[1:]:
    cell = table.select_one('td').get_text(strip=True)
    print(cell)

输出:

Vegetable Basket
Russian Nesting Dolls
Fish Painting
Dead Parrot
Mystery Box
© www.soinside.com 2019 - 2024. All rights reserved.