【发布时间】:2021-06-07 11:18:05
【问题描述】:
我正在尝试创建一个抓取下载链接的刮板,我想使用正则表达式,但这对我来说将是一场噩梦,我找到了这个名为 BeautifulSoup 的库,我正在尝试捕获div class="article-content" 的子项中的 url,即<p> 标签,这个<h3> 是 url 的名称,我不想将所有 url 合并到一个列表中,而是使用字典,这是关键name(<h3>) 和 value 是 url 列表,这里的代码就够了。
import requests
from bs4 import BeautifulSoup
def scrape():
resp = requests.get('https://www.animeout.xyz/love-live-nijigasaki-gakuen-school-idol-doukoukai-1080p-300mb720p-150mbepisode-1/')
soup = BeautifulSoup(resp.text,'html.parser')
contents = soup.find('div',class_='article-content')
output = {}
for tag in contents.children:
if tag.name == 'h3':
name = tag.text
links = []
for sibling in tag.next_siblings:
if sibling.name == 'p':
for link in sibling.find_all('a',text='Direct Download'):
links.append(link.get('href'))
if sibling.name == 'h3':
output.update({name:links})
break
到目前为止,我只设法捕获了 1 个密钥,有没有 Pythonic 方法可以做到这一点?
【问题讨论】:
-
请将标题更改为对未来面临类似问题的用户有用的内容
-
没问题,我已经编辑了标题。
标签: python html for-loop beautifulsoup python-requests