我有一个小项目,试图从网页上下载一系列壁纸。我是python的新手。
我正在使用该urllib
库,该库将返回一长串的网页数据,其中包括
<a href="http://website.com/wallpaper/filename.jpg">
我知道我需要下载的每个文件名都有
'http://website.com/wallpaper/'
如何在页面源中搜索这部分文本,并返回其余图像链接(以“ * .jpg”扩展名结尾)?
r'http://website.com/wallpaper/ xxxxxx .jpg'
我在想是否可以格式化xxxx部分不评估的正则表达式?只需检查路径和.jpg扩展名即可。找到匹配项后返回整个字符串
我在正确的轨道上吗?
BeautifulSoup对于此类事情非常方便。
import re
import urllib3
from bs4 import BeautifulSoup
jpg_regex = re.compile('\.jpg$')
site_regex = re.compile('website\.com\/wallpaper\/')
pool = urllib3.PoolManager()
request = pool.request('GET', 'http://your_website.com/')
soup = BeautifulSoup(request)
jpg_list = list(soup.find_all(name='a', attrs={'href':jpg_regex}))
site_list = list(soup.find_all(name='a', attrs={'href':site_regex}))
result_list = map(lambda a: a.get('href'), jpg_list and site_list)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句