垂直抓取表格

首先让我说我讨厌问一个非常盲目的“请为我写它”式的问题,因为我自己通常喜欢学习这样的东西,但我时间紧迫。最重要的是,昨天是我第一次得知 BeautifulSoup 的存在,而且我已经有大约 7 年没有编写任何真正的脚本或编码了,所以我来了。


简而言之,我正在尝试创建一个抓取工具来遍历包含垂直排列的表格的多个页面,并将数据输出到 csv。因此,在第一遍中,我需要读取第一列以在我的 CSV 中创建标题行,然后对于其余 URL,只需读取第二列以创建数据行。我想我可以弄清楚如何使用 URL 遍历输入文件,但如果有人对此有快速了解,我的计划是一个输入文件,每行都有一个 url。


该表的编码非常糟糕,有大量的空格和回车符,但我想我已经处理好了。我被卡住的地方是垂直循环遍历列。为了方便起见,表结构如下所示:


<div id="mydiv">

    <table>

      <tr>

        <td>header 1</td>

        <td>value 1</td>

      </tr>

      <tr>

        <td>header 2</td>

        <td>value 2</td>

      </tr>

      <tr>

        <td>header 3</td>

        <td>value 3</td>

      </tr>

    </table>

</div>

昨天我在 google 上玩了 30 分钟,把我带到了可以将表格导出为 CSV 并删除所有多余字符的地方。它是页面上的第一个,没有 ID,但到目前为止我写的只是输出那个表。表所在的 DIV 似乎有一个唯一的 ID,所以如果涉及到它,我可能可以隔离它。


from bs4 import BeautifulSoup

import requests

import csv

url="https://myurl.com/"

html = requests.get(url).text


soup = BeautifulSoup(html, "lxml")

table = soup.find("table")


output_rows = []

for table_row in table.findAll('tr'):

    columns = table_row.findAll('td')

    output_row = []

    for column in columns:

         output_row.append(' '.join(column.text.split()))

    output_rows.append(output_row)


with open('output.csv', 'w') as csvfile:

    writer = csv.writer(csvfile)

    writer.writerows(output_rows)

这让我把表格刮成水平格式(尽管 CSV 中每行数据之间有一条额外的线,我还没有弄清楚)。但我需要的是跨多个 URL 垂直抓取它,最后得到一个如下所示的 CSV:


header1,header2,header3

value1,value2,value3

value1,value2,value3 (from next URL in the list)

value1,value2,value3 (from next url in the list and so on)


12345678_0001
浏览 83回答 1
1回答

浮云间

没有更多信息,我想这会对您有所帮助:from bs4 import BeautifulSoupimport pandas as pdhtml = '''<div id="mydiv">&nbsp; &nbsp; <table>&nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; <td>header 1</td>&nbsp; &nbsp; &nbsp; &nbsp; <td>value 1</td>&nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; <td>header 2</td>&nbsp; &nbsp; &nbsp; &nbsp; <td>value 2</td>&nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; <td>header 3</td>&nbsp; &nbsp; &nbsp; &nbsp; <td>value 3</td>&nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; </table></div>'''num = 0headers = []values = []rows = []while True:&nbsp; &nbsp; soup = BeautifulSoup(html, 'html.parser')&nbsp; &nbsp; trs = soup.select('div#mydiv tr')&nbsp; &nbsp; for t in trs:&nbsp; &nbsp; &nbsp; &nbsp; for header, value in zip(t.select('td')[0], t.select('td')[1]):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if num == 0:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; headers.append(header)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; values.append(value)&nbsp; &nbsp; rows.append(values)&nbsp; &nbsp; values = []&nbsp; &nbsp; num += 1&nbsp; &nbsp; if num > 50:&nbsp; &nbsp; &nbsp; &nbsp; breakdf = pd.DataFrame(rows, columns= headers)print(df.head())df.to_csv('mycsv.csv')印刷:&nbsp; header 1 header 2 header 30&nbsp; value 1&nbsp; value 2&nbsp; value 31&nbsp; value 1&nbsp; value 2&nbsp; value 32&nbsp; value 1&nbsp; value 2&nbsp; value 33&nbsp; value 1&nbsp; value 2&nbsp; value 34&nbsp; value 1&nbsp; value 2&nbsp; value 3依此类推...并将数据保存到名为mycsv.csv.csv 文件前 30 行在此代码中,我在您的示例 html 代码上运行了 50 次循环,以生成更多的数据值。但是你需要为每一页提出一个新的请求并对其进行补充,但我想你明白了。这只是您如何编写代码的 POC。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python