繁体   English   中英

无法弄清楚如何使用beautifulsoup进行网络抓取

[英]Can't figure how to web-scraping using beautifulsoup

我正在尝试从某些网页上抓取以下信息。 这是完整的代码:

<tr class="owner">
   <td id="P184" class="ownerP" colspan="4">
      <ul>
         <li><span class="detailType">name:</span><span class="detail">merry/span></li>
         <li><a title="sendmessage" class="sendMessageLink" onclick="return openSendMessage('/sendMessage.php',20205" href="" tabindex="0"><span></span>sendmessage</a>&nbsp;<span class="remark_soft">(by pm system)</span></li>
         <li><span class="detailType">phone 1</span><a class="detail" href="tel:0387362531">0387362531</a></li>
         <li><span class="detailType"></span></li>
      </ul>
   </td>
</tr>

我只想获取此信息(电话号码):

 <a class="detail" href="tel:0387362531">0387362531</a> 

这是我的代码,但是不起作用:

 for details in soup.find_all(attrs= {"class": "detail"}):
    re_res = re.search(r"tel:\('.*?',(\d+)\)", details['href'])
    print(re_res)

您非常接近,在这里您可以:

import re
from bs4 import BeautifulSoup

html_doc = """
<tr class="owner"><td id="P184" class="ownerP" colspan="4"><ul>
            <li><span class="detailType">name:</span><span class="detail">merry/span></li>
            <li><a title="sendmessage" class="sendMessageLink" onclick="return openSendMessage('/sendMessage.php',20205" href="" tabindex="0"><span></span>sendmessage</a>&nbsp;<span class="remark_soft">(by pm system)</span></li><li><span class="detailType">phone 1</span><a class="detail" href="tel:0387362531">0387362531</a></li><li><span class="detailType"></span></li>
</ul></td></tr>
"""

soup = BeautifulSoup(html_doc, 'html.parser')

for details in soup.find_all(attrs= {"class": "detail"}):
    if "href" in details.attrs and re.search("^tel:", details.attrs["href"]):
        print(details.text)

输出:

0387362531

我只是在浏览您创建的详细信息列表,如果我发现有一个具有hrefhreftel:开头的列表,然后打印出该值。

您应该将soup.find_all(attrs= {"class": "detail"}) soup.find_all('a', attrs= {"class": "detail"})[0] ,以避免产生spandetails

而且您的正则表达式不起作用,这一行应该起作用tel:(\\d+) 但是,而不是使用正则表达式为什么不干脆让a做标记文字details.text

您必须将元素类型a添加到find_all,并且您的正则表达式tel:\\('.*?',(\\d+)\\)尝试匹配href 左括号括号\\(\\)

您可以将正则表达式更新为tel:(\\d+)以匹配tel:后跟捕获组(组1)中的一个或多个数字,可以使用re_res.group(1)检索。

例如:

for details in soup.find_all('a', attrs= {"class": "detail"}):
    re_res = re.search(r"tel:(\d+)", details['href'])
    print(re_res.group(1))  # 0387362531

您无需使用正则表达式即可获得相同的结果。 在这种情况下,请尝试以下方法:

from bs4 import BeautifulSoup

html_doc = """
<tr class="owner"><td id="P184" class="ownerP" colspan="4"><ul>
            <li><span class="detailType">name:</span><span class="detail">merry/span></li>
            <li><a title="sendmessage" class="sendMessageLink" onclick="return openSendMessage('/sendMessage.php',20205" href="" tabindex="0"><span></span>sendmessage</a>&nbsp;<span class="remark_soft">(by pm system)</span></li><li><span class="detailType">phone 1</span><a class="detail" href="tel:0387362531">0387362531</a></li><li><span class="detailType"></span></li>
</ul></td></tr>
"""

使用.select()

soup = BeautifulSoup(html_doc, 'html.parser')
for telephone in soup.select("a[href^='tel:']"):
    if "detail" in telephone['class']:
        print(telephone.text)

或使用.find_all()

soup = BeautifulSoup(html_doc, 'html.parser')
for telephone in soup.find_all("a",class_="detail"):
    if telephone['href'].startswith('tel:'):
        print(telephone.text)

它们都产生相同的输出:

0387362531

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM