【发布时间】:2020-05-11 19:41:28
【问题描述】:
我正在尝试从an Apple wikipedia page 对表格及其内容进行网络抓取。我正在使用 Beautiful Soup 来提取数据。我有以下代码:
from bs4 import BeautifulSoup
appleurl="https://en.m.wikipedia.org/wiki/Timeline_of_Apple_Inc._products"
import requests
import pandas as pad
import lxml.html as html
_content = requests.get(appleurl)
soup = BeautifulSoup(_content.content)
_table = soup.findChildren('table')
rows = _table[0].findChildren(['th','tr'])
for row in rows:
cells = row.findChildren('td')
for cell in cells:
value = cell.string
print ("The value in this cell is %s"% value)
我有以下价值观:
The value in this cell is 1976
The value in this cell is April 11
The value in this cell is Apple I
The value in this cell is Apple I
The value in this cell is September 1, 1977
The value in this cell is 1977
The value in this cell is April 1
The value in this cell is Apple II
The value in this cell is Apple II
The value in this cell is June 1, 1979
The value in this cell is 1978
The value in this cell is June 1
The value in this cell is Disk II
The value in this cell is Drives
The value in this cell is May 1, 1984
The value in this cell is 1979
The value in this cell is June 1
The value in this cell is Apple II Plus
The value in this cell is Apple II series
The value in this cell is December 1, 1982
The value in this cell is None
The value in this cell is None
The value in this cell is None
The value in this cell is Bell & Howell Disk II
The value in this cell is None
The value in this cell is Apple SilenType
The value in this cell is Printers
The value in this cell is October 1, 1982
问题在于,对于 1979 年,模型的数量是多个,在我的情况下没有被提取。我需要当年的所有模型1979。如果每年只有一行,我的代码可以很好地提取。如果在我提供的链接的第一个表中一年有多行,我该怎么办。
我需要的值是年份、发布日期、型号。其他两列可以去掉。
我将非常感谢您的帮助。
【问题讨论】:
标签: python pandas web-scraping beautifulsoup