在Python中使用BeautifulSoup搜索html

2024-09-29 05:21:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我写了一些代码来搜索html,但是结果不是我想要的。 一些html代码我想拉页面地址 我想知道“sayfa”这个词 示例:

http://www.vbulletin.com.tr/vbulletin-temel-bilgiler/sayfa2

http://www.vbulletin.com.tr/vbulletin-temel-bilgiler/sayfa3

但我不知道怎么做

<table cellpadding="0" cellspacing="0" border="0" width="100%" style="margin-bottom:3px">
<tr valign="bottom">
    <td class="smallfont"><a href="http://www.vbulletin.com.tr/newthread.php?do=newthread&amp;f=16" rel="nofollow"><img src="http://www.vbulletin.com.tr/images/fsimg/butonlar/newthread.gif" alt="Yeni Konu Oluştur" border="0" /></a></td>
    <td align="right"><div class="pagenav" align="right">
<table class="tborder" cellpadding="3" cellspacing="1" border="0">
<tr>
    <td class="vbmenu_control" style="font-weight:normal">Sayfa 1 Toplam 5 Sayfadan</td>


        <td class="alt2"><span class="smallfont" title="Toplam 100 sonuçtan 1 ile 20 arası sonuç gösteriliyor."><strong>1</strong></span></td>
 <td class="alt1"><a class="smallfont" href="http://www.vbulletin.com.tr/vbulletin-temel-bilgiler/sayfa2/" title="Toplam 100 sonuçtan 21 ile 40 arası sonuç gösteriliyor.">2</a></td><td class="alt1"><a class="smallfont" href="http://www.vbulletin.com.tr/vbulletin-temel-bilgiler/sayfa3/" title="Toplam 100 sonuçtan 41 ile 60 arası sonuç gösteriliyor.">3</a></td>
    <td class="alt1"><a rel="next" class="smallfont" href="http://www.vbulletin.com.tr/vbulletin-temel-bilgiler/sayfa2/" title="Sonraki Sayfa - Toplam 100 sonuçtan 21 ile 40 arası sonuç gösteriliyor.">&gt;</a></td>
    <td class="alt1" nowrap="nowrap"><a class="smallfont" href="http://www.vbulletin.com.tr/vbulletin-temel-bilgiler/sayfa5/" title="Sonuncu Sayfa - Toplam 100 sonuçtan 81 ile 100 arası sonuç gösteriliyor.">Son Sayfa <strong>&raquo;</strong></a></td>
    <td class="vbmenu_control" title="forumdisplay.php?f=16&amp;order=desc"><a name="PageNav"></a></td>
</tr>
</table>
</div></td>
</tr>
</table>

我想坐“href”

^{pr2}$

Tags: comhttptitlewwwtrclasstdhref
3条回答

试试这个

from BeautifulSoup import BeautifulSoup
import requests
domain = "http://www.forumsokagi.com/peygamber-ve-evliyalarin-hayatlari/"
page = requests.get(domain)
result = BeautifulSoup(page.text)
anc = result.findAll("span")
for values in range(len(anc)):
    anchor = anc[values].findAll('a')
    for i in anchor:
        if "javascript" not in i.get('href') and "sayfa" in i.get('href'):
            print i.get('href')

这将为您获取href链接。在

^{pr2}$
for span in soup.findAll('span'):
   if span.a:
       print span.a["href"]

在列表组件中:

^{pr2}$

如果print span.a在循环中,您将看到None,因此您需要在使用span.a["href"]之前检查{},否则您将得到一个TypeError: 'NoneType' object has no attribute '__getitem__'

您可以使用set comp,因为存在重复的URL:

urls = {span.a["href"] for span in soup.findAll('span') if span.a}

然后搜索您需要的任何url:

for url in sorted(urls):
    if "sayfa" in url:
        print url
http://www.forumsokagi.com/peygamber-ve-evliyalarin-hayatlari/sayfa2/
http://www.forumsokagi.com/peygamber-ve-evliyalarin-hayatlari/sayfa3/
http://www.forumsokagi.com/peygamber-ve-evliyalarin-hayatlari/sayfa4/
http://www.forumsokagi.com/peygamber-ve-evliyalarin-hayatlari/sayfa7/

In [26]: import urllib2

In [27]: from bs4 import BeautifulSoup

In [28]: domain="http://www.forumsokagi.com/peygamber-ve-evliyalarin-hayatlari/" 
In [29]: page = urllib2.urlopen(domain).read()

In [30]: soup = BeautifulSoup(page)

In [31]: urls = {span.a["href"] for span in soup.findAll('span') if span.a}

In [32]: for url in sorted(urls):
   ....:     if "sayfa" in url:
   ....:             print url
   ....:         
http://www.forumsokagi.com/peygamber-ve-evliyalarin-hayatlari/sayfa2/
http://www.forumsokagi.com/peygamber-ve-evliyalarin-hayatlari/sayfa3/
http://www.forumsokagi.com/peygamber-ve-evliyalarin-hayatlari/sayfa4/
http://www.forumsokagi.com/peygamber-ve-evliyalarin-hayatlari/sayfa7/

假设您希望URL包含wordsayfa。在

您也可以使用lxml来执行此操作。在

import urllib2
import lxml.html
domain="http://www.forumsokagi.com/peygamber-ve-evliyalarin-hayatlari/"
data=urllib2.urlopen(domain).read()
tree = lxml.html.fromstring(data)
for i in  tree.xpath('//a/@href'):
    if "sayfa" in i:
        print i

输出:

^{pr2}$

相关问题 更多 >