在Python中使用BeautifulSoup进行列表的Web抓取

2024-09-28 21:19:25 发布

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

我是Python的新手,正在学习如何使用beauthoulsoup来抓取网页。首先,我只是用雅虎的HTML代码:

查看源:https://www.yahoo.com/

我想从第577行到633行的链接列表中获取它们的URL和标题,并将其放入Python中的表中。在

到目前为止,我有以下情况:

from bs4 import BeautifulSoup

myURL = "http://www.yahoo.com"
myPage = requests.get(myURL)

yahoo = BeautifulSoup(myPage.content)

print yahoo.prettify()

YahooList = yahoo.find('ul', class_="Pos(r) Miw(1000px) Pstart(9px) Lh(1.7) Reader-open_Op(0) mini-header_Op(0)")
print YahooList

我不知道该如何继续下去。我找到的所有例子都是为了从表格中抓取网页,但我在列表中找不到太多的例子。在

有人有什么建议吗?在

谢谢, 尼克


Tags: com网页列表htmlwwwyahoo例子print
1条回答
网友
1楼 · 发布于 2024-09-28 21:19:25

如果你只需要刮去特定的线,你需要先把这些线刮到。我建议使用^{}列表切片来获取它们。在

例如:

>>> import requests
>>> from bs4 import BeautifulSoup
>>> r = requests.get('http://www.yahoo.com')
>>> print('\n'.join(r.text.splitlines()[575:634]))

输出为:

^{pr2}$
  • r.text.splitlines()按行拆分HTML源代码,并给出一个列表。

  • [575:634]是一个列表切片,它对列表进行切片,并给出从576634的行。我又添加了两行,因为没有它们,输出将是:

        <a href="https://www.yahoo.com/politics/" class="D(b) Fz(13px) C($topbarMenu) Py(3px) Td(n) Td(u):h"   data-ylk="slk:politics;t5:politics;cpos:9;" tabindex="1">Politics</a>
    </li>
    
    <li class="D(b)">
        <a href="https://www.yahoo.com/celebrity/" class="D(b) Fz(13px) C($topbarMenu) Py(3px) Td(n) Td(u):h"   data-ylk="slk:celebrity;t5:celebrity;cpos:10;" tabindex="1">Celebrity</a>
    </li>
    
    ...
    
    <li class="D(b)">
        <a href="https://www.yahoo.com/travel/" class="D(b) Fz(13px) C($topbarMenu) Py(3px) Td(n) Td(u):h"   data-ylk="slk:travel;t5:travel;cpos:22;" tabindex="1">Travel</a>
    </li>
    
    <li class="D(b)">
        <a href="https://www.yahoo.com/autos/" class="D(b) Fz(13px) C($topbarMenu) Py(3px) Td(n) Td(u):h"   data-ylk="slk:autos;t5:autos;cpos:23;" tabindex="1">Autos</a>
    

    这不是一个有效的HTML代码块。

  • ^{}通过\n加入列表,并给出另一个所需的字符串。


在我们有了特定的线路之后:

>>> soup = BeautifulSoup('\n'.join(r.text.splitlines()[575:634]), 'html.parser')
>>> for i in soup.find_all('a'):
...     print(i.get('href'))
...     
... 
https://www.yahoo.com/politics/
https://www.yahoo.com/celebrity/
https://www.yahoo.com/movies/
https://www.yahoo.com/music/
https://www.yahoo.com/tv/
https://www.yahoo.com/health/
https://www.yahoo.com/style/
https://www.yahoo.com/beauty/
https://www.yahoo.com/food/
https://www.yahoo.com/parenting/
https://www.yahoo.com/makers/
https://www.yahoo.com/tech/
https://shopping.yahoo.com/
https://www.yahoo.com/travel/
https://www.yahoo.com/autos/

soup.find_all('a')查找字符串(HTML代码块)中的所有<a>HTML标记,并给出这些标记的列表。在

{{{cd8>使用


也可以使用list comprehension将结果放入列表中,而不是打印出来:

import requests
from bs4 import BeautifulSoup

r = requests.get('http://www.yahoo.com')
soup = BeautifulSoup('\n'.join(r.text.splitlines()[575:634]), 'html.parser')

l = [i.get('href') for i in soup.find_all('a')]

l是您要查找的列表。在


如果您还想获得这些链接的标题,可以使用i.text来获取。但是,Python中没有table object,我想您的意思是^{}

>>> d = {i.text: i.get('href') for i in soup.find_all('a')}
>>> pprint(d)
{'Autos': 'https://www.yahoo.com/autos/',
 'Beauty': 'https://www.yahoo.com/beauty/',
 'Celebrity': 'https://www.yahoo.com/celebrity/',
 'Food': 'https://www.yahoo.com/food/',
 'Health': 'https://www.yahoo.com/health/',
 'Makers': 'https://www.yahoo.com/makers/',
 'Movies': 'https://www.yahoo.com/movies/',
 'Music': 'https://www.yahoo.com/music/',
 'Parenting': 'https://www.yahoo.com/parenting/',
 'Politics': 'https://www.yahoo.com/politics/',
 'Shopping': 'https://shopping.yahoo.com/',
 'Style': 'https://www.yahoo.com/style/',
 'TV': 'https://www.yahoo.com/tv/',
 'Tech': 'https://www.yahoo.com/tech/',
 'Travel': 'https://www.yahoo.com/travel/'}
>>> d['TV']
'https://www.yahoo.com/tv/'
>>> d['Food']
'https://www.yahoo.com/food/'

所以您可以使用{i.text: i.get('href') for i in soup.find_all('a')}来获得您想要的dict。在

在本例中,i.text(title)是该dict中的键,例如'TV'和{}。在

并且i.get('href')是值(链接),例如'https://www.yahoo.com/tv/'和{}。在

您可以通过d[key]访问该值,就像我上面的代码一样。在

相关问题 更多 >