Python 用BeautifulSoup刮一系列桌子
我正在努力学习web抓取和python(以及编程),并找到了BeautifulSoup库,它似乎提供了很多可能性 我试图找出如何最好地从本页中提取相关信息: 我可以更详细地介绍这一点,但基本上是公司名称、相关描述、联系方式、各种公司详细信息/统计数据e.t.c 在这一阶段,我们将研究如何干净地隔离这些数据并对其进行刮取,以便在以后将其全部放入CSV或其他文件中 我不知道如何使用BS来获取不同的表数据。有很多tr和td标签,不知道如何定位到任何独特的东西上 我想到的最好的开始代码是:Python 用BeautifulSoup刮一系列桌子,python,beautifulsoup,Python,Beautifulsoup,我正在努力学习web抓取和python(以及编程),并找到了BeautifulSoup库,它似乎提供了很多可能性 我试图找出如何最好地从本页中提取相关信息: 我可以更详细地介绍这一点,但基本上是公司名称、相关描述、联系方式、各种公司详细信息/统计数据e.t.c 在这一阶段,我们将研究如何干净地隔离这些数据并对其进行刮取,以便在以后将其全部放入CSV或其他文件中 我不知道如何使用BS来获取不同的表数据。有很多tr和td标签,不知道如何定位到任何独特的东西上 我想到的最好的开始代码是: from
from bs4 import BeautifulSoup
import urllib2
html = urllib2.urlopen("http://www.aidn.org.au/Industry-ViewCompany.asp?CID=3113")
soup = BeautifulSoup(html)
soupie = soup.prettify()
print soupie
然后使用regex e.t.c.从清理后的文本中提取数据
但一定有更好的方法使用BS树来实现这一点?或者这个网站的格式是BS不会提供更多帮助的吗
不寻找完整的解决方案,因为这是一个大问题,我想学习,但任何代码片段,让我在我的方式将不胜感激
更新
感谢下面的@ZeroPiraeus,我开始理解如何解析表格。以下是他的代码的输出:
=== Personnel ===
bodytext Ms Gail Morgan CEO
bodytext Phone: +61.3. 9464 4455 Fax: +61.3. 9464 4422
bodytext Lisa Mayoh Sales Manager
bodytext Phone: +61.3. 9464 4455 Fax: +61.3. 9464 4422 Email: bob@aerospacematerials.com.au
=== Company Details ===
bodytext ACN: 007 350 807 ABN: 71 007 350 807 Australian Owned Annual Turnover: $5M - $10M Number of Employees: 6-10 QA: ISO9001-2008, AS9120B, Export Percentage: 5 % Industry Categories: AerospaceLand (Vehicles, etc)LogisticsMarineProcurement Company Email: lisa@aerospacematerials.com.au Company Website: http://www.aerospacematerials.com.au Office: 2/6 Ovata Drive Tullamarine VIC 3043 Post: PO Box 188 TullamarineVIC 3043 Phone: +61.3. 9464 4455 Fax: +61.3. 9464 4422
paraheading ACN:
bodytext 007 350 807
paraheading ABN:
bodytext 71 007 350 807
paraheading
bodytext Australian Owned
paraheading Annual Turnover:
bodytext $5M - $10M
paraheading Number of Employees:
bodytext 6-10
paraheading QA:
bodytext ISO9001-2008, AS9120B,
paraheading Export Percentage:
bodytext 5 %
paraheading Industry Categories:
bodytext AerospaceLand (Vehicles, etc)LogisticsMarineProcurement
paraheading Company Email:
bodytext lisa@aerospacematerials.com.au
paraheading Company Website:
bodytext http://www.aerospacematerials.com.au
paraheading Office:
bodytext 2/6 Ovata Drive Tullamarine VIC 3043
paraheading Post:
bodytext PO Box 188 TullamarineVIC 3043
paraheading Phone:
bodytext +61.3. 9464 4455
paraheading Fax:
bodytext +61.3. 9464 4422
我的下一个问题是,什么是将这些数据放入CSV的最佳方式,该CSV适合导入电子表格?例如,将“ABN”、“ACN”、“公司网站”e.t.c.等内容作为列标题,然后将相应的数据作为行信息
谢谢你的帮助。我曾经在这条路上走过一次。我使用的html页面总是与表的格式相同,并且是公司内部的。我们确保客户知道,如果他们更改页面,很可能会中断编程。有了这个规定,我们就可以通过tr和td列表中的索引值来确定所有东西的位置。这远非拥有他们无法或不愿提供的XML数据的理想情况,但已经完美地工作了近一年。如果有人知道更好的答案,我也想知道。这是我第一次也是唯一一次使用Beautiful Soup,从那以后我就再也不需要它了,但它工作得很好。您的代码将完全取决于您想要什么以及如何存储它,但这段代码应该让您了解如何从页面中获取相关信息:
import requests
from bs4 import BeautifulSoup
url = "http://www.aidn.org.au/Industry-ViewCompany.asp?CID=3113"
html = requests.get(url).text
soup = BeautifulSoup(html)
for feature_heading in soup.find_all("td", {"class": "Feature-Heading"}):
print "\n=== %s ===" % feature_heading.text
details = feature_heading.find_next_sibling("td")
for item in details.find_all("td", {"class": ["bodytext", "paraheading"]}):
print("\t".join([item["class"][0], " ".join(item.text.split())]))
我发现一个比urllib2
更令人愉快的库,当然这取决于您
编辑:
在回答您的后续问题时,您可以使用以下内容来从所收集的数据编写CSV文件:
import csv
import requests
from bs4 import BeautifulSoup
columns = ["ACN", "ABN", "Annual Turnover", "QA"]
urls = ["http://www.aidn.org.au/Industry-ViewCompany.asp?CID=3113", ] # ... etc.
with open("data.csv", "w") as csv_file:
writer = csv.DictWriter(csv_file, columns)
writer.writeheader()
for url in urls:
soup = BeautifulSoup(requests.get(url).text)
row = {}
for heading in soup.find_all("td", {"class": "paraheading"}):
key = " ".join(heading.text.split()).rstrip(":")
if key in columns:
next_td = heading.find_next_sibling("td", {"class": "bodytext"})
value = " ".join(next_td.text.split())
row[key] = value
writer.writerow(row)
非常感谢@ZeroPiraeus。这大大有助于我制定出使用BS进行此类工作的策略。如果你不介意看一看的话,我已经用一个更进一步的问题修改了我的问题?不客气。。。但我想你的编辑一定被弄坏了;现在它只是说“谢谢”。是的,我不得不回到它。现在应该到了。哇,你在这里真的给了我很多帮助@ZeroPiraeus!非常感谢。那么,你大概会在每一页上划一行吧?这是个好主意,是的。