栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

BeautifulSoup:从锚标记中提取文本

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

BeautifulSoup:从锚标记中提取文本

以上所有答案确实可以帮助我构建答案,因此,我对其他用户提出的所有答案投了赞成票:但是我最终对自己正在处理的确切问题汇总了自己的答案:

正如明确定义的问题一样,我必须以dom结构访问某些兄弟姐妹及其子代:此解决方案将迭代dom结构中的图像,并使用产品标题构造图像名称,并将图像保存到本地目录。

    import urlparse    from urllib2 import urlopen    from urllib import urlretrieve    from BeautifulSoup import BeautifulSoup as bs    import requests    def getImages(url):        #Download the images        r = requests.get(url)        html = r.text        soup = bs(html)        output_folder = '~/amazon'        #extracting the images that in div(s)        for div in soup.findAll('div', attrs={'class':'image'}): modified_file_name = None try:     #getting the data div using findNext     nextDiv =  div.findNext('div', attrs={'class':'data'})     #use findNext again on previous object to get to the anchor tag     fileName = nextDiv.findNext('a').text     modified_file_name = fileName.replace(' ','-') + '.jpg' except TypeError:     print 'skip' imageUrl = div.find('img')['src'] outputPath = os.path.join(output_folder, modified_file_name) urlretrieve(imageUrl, outputPath)    if __name__=='__main__':        url = r'http://www.amazon.com/s/ref=sr_pg_1?rh=n%3A172282%2Ck%3Adigital+camera&keywords=digital+camera&ie=UTF8&qid=1343600585'        getImages(url)


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/633705.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号