我正在抓取这个页面(“http://mahaprantikssksamaj.com/ssk-samaj-maharashtras.aspx"”)。我存储有效的urls并请求重定向到下一页,并为每个有效的urls刮掉下一页的数据。
页面的数据存储在表中,我得到了这个错误:“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)打印(表)给出了这一点:
<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文件中。谁能事先帮我找到我的wrong.Thanks吗?
发布于 2018-08-29 18:05:28
要从登陆页面中连接到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']发布于 2018-08-29 17:15:08
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
当我们检查网站时,它显示table有tbody。但它可能在源代码中不可用。要确认这一点,请访问view page source。
https://stackoverflow.com/questions/52082312
复制相似问题