【问题标题】:Scraping Webpage using href links使用 href 链接抓取网页
【发布时间】:2018-08-29 16:38:11
【问题描述】:

我正在抓取此页面(“http://mahaprantikssksamaj.com/ssk-samaj-maharashtras.aspx”)。我正在存储有效 url 并请求重定向到下一页并为每个有效 url 抓取下一页的数据。

页面的数据存储在表中,我收到此错误: ""AttributeError: ResultSet 对象没有属性'find'。您可能将项目列表视为单个项目。当您打算调用 find() 时,您是否调用了 find_all()? “” 我的代码在这里:

 from bs4 import BeautifulSoup
 import requests
  r = requests.get('http://mahaprantikssksamaj.com/ssk-samaj-maharashtras.aspx')
  soup = BeautifulSoup(r.text, 'html.parser')
      for i in range(36):
      print(i)
      url = 'http://mahaprantikssksamaj.com/ssk-prantik-members.aspx?id={}'.format(i)
      r = requests.get(url)
      web = BeautifulSoup(r.content,"html.parser")
      table= web.findAll("table",id="DGORG") 
      print(table)
      table_body = table.find('tbody')
      rows = table_body.find_all('tr')
          for tr in rows:
           cols = tr.find_all('td')
           for td in cols:
              print (td)

print(table) 给出 o/p 这个:

  <div class="memcss">
  <table  border="1" style="width:90%;padding:10px;margin:0px 0px 20px 
  20px;box-shadow:2px 2px 2px #000000">
  <tr>
  <td colspan="2" style="text-align:center"><h5>Mr. Jaydeo Mahadeosa 
  Pawar</h5></td>
  </tr>
  <tr>
  <td colspan="2" style="text-align:center"><h6>Secretory</h6></td>
  </tr>
  <tr>
  <td style="width:25%;height:30px;text-align:right">Address : </td>
  <td> Pune</td>
  </tr>
  <tr>
  <td style="width:20%;height:30px;text-align:right">City : </td>
  <td> Pune</td>
  </tr>
  <tr>
  <td style="width:20%;height:30px;text-align:right">Mobile : </td>
  <td> </td>
  </tr>
  </table>
  </div>

  </td>
  </tr><tr>
  <td>

试图在 csv 文件中仅存储姓名、职务、地址和手机号码。任何人都可以在我错的地方帮忙。在此先感谢。

【问题讨论】:

标签: python web-scraping beautifulsoup


【解决方案1】:

要从登录页面中连接到view members链接的每个表格中获取所有内容,您可以遵循以下方法:

from bs4 import BeautifulSoup
from urllib.parse import urljoin
import requests

link = "http://mahaprantikssksamaj.com/ssk-samaj-maharashtras.aspx"

res = requests.get(link)
soup = BeautifulSoup(res.text, 'html.parser')
for item in soup.select("a[style$='text-decoration:none']"):
    req = requests.get(urljoin(link,item.get("href")))
    sauce = BeautifulSoup(req.text,"html.parser")
    for elem in sauce.select(".memcss table tr"):
        data = [item.get_text(strip=True) for item in elem.select("td")]
        print(data)

输出如下:

['Shri. Narsinhasa Narayansa Kolhapure']
['Chairman']
['Address :', 'Ahamadnagar']
['City :', 'Ahamadnagar']
['Mobile :', '2425577']

【讨论】:

  • 感谢您的回答。再次感谢您的帮助。
  • 如何将输出保存在 csv 文件中如果您能帮助我,那就太好了。
【解决方案2】:
from bs4 import BeautifulSoup
import requests

r = requests.get('http://mahaprantikssksamaj.com/ssk-samaj-maharashtras.aspx')
soup = BeautifulSoup(r.text, 'html.parser')
for i in range(36):
    print(i)
    url = 'http://mahaprantikssksamaj.com/ssk-prantik-members.aspx?id={}'.format(i)
    r = requests.get(url)
    web = BeautifulSoup(r.content, "html.parser")
    table = web.find("table", id="DGORG")
    print(table)
    rows = table.find_all('tr')
    for tr in rows:
        cols = tr.find_all('td')
        for td in cols:
            print(td)

变化

使用table= web.findAll("table",id="DGORG") 使用find insted of findAll

当我们检查网站时,它显示tabletbody。但它可能在源代码中不可用。要确认,请转到view page source

how to get tbody from table from python beautiful soup ?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-29
    • 1970-01-01
    • 2020-08-08
    • 1970-01-01
    • 1970-01-01
    • 2021-03-01
    相关资源
    最近更新 更多