【问题标题】:How do I get information from a table into variables while using BeautifulSoup 4?使用 BeautifulSoup 4 时如何将表中的信息转换为变量?
【发布时间】:2015-01-26 22:07:55
【问题描述】:

我有一个表格,在下面找到并存储为“表格”。它包含以下内容:

http://pastebin.com/aBFLpU4U

我的代码捕获了正确的信息,但我需要知道如何将每条信息放入它自己的变量中。感谢您对此提供的任何帮助,我只玩 BeautifulSoup 一个星期,所以请原谅我。我翻遍了整个堆栈,但没有找到适合我的答案。

这是我看到的输出:http://pastebin.com/fiYQvBix

import sys, locale, os, re, urllib2
import lxml.etree, requests
from bs4 import BeautifulSoup as bSoup

# Website that we are scraping:
BASE_URL = 'https://www.biddergy.com/detail.asp?id='

#ID = raw_input("Enter listing #: ")
ID = str(330998) # defined constant for debugging
# Store response in soup:
response  = requests.get(BASE_URL+ID)
soup = bSoup(response.text)

# Find auction info <table>
table = soup.find('table', cellpadding="2")

#### Everything above this line works great ####

for row in table.find_all('tr'):
    for col in row.find_all("td"):
        print(col.string)

【问题讨论】:

    标签: python web-scraping beautifulsoup html-table


    【解决方案1】:

    好吧,我想通了。

    data = []
    for row in table.find_all('tr'):
        for cols in row.find_all('td', text=True)
            for col in cols:
                data.append(col.strip())
    

    然后可以从 data[] 列表中提取数据并保存到相应的变量中。

    感谢所有阅读我问题的人!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-08-18
      • 2012-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-09
      相关资源
      最近更新 更多