python 对指定URL获取其子链接
生活随笔
收集整理的這篇文章主要介紹了
python 对指定URL获取其子链接
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
仿照http://blog.csdn.net/lming_08/article/details/44710779里面的方法, 獲取指定URL 的所需的子鏈接及其描述.
#!/usr/bin/python # -*- coding: utf-8 -*- import sys import urllib2 import reif len(sys.argv) != 2:print "%s url" % __file__sys.exit(-1)url=sys.argv[1]user_agent = 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6' headers = { 'User-Agent' : user_agent } ''' <a href="http://faxian.smzdm.com/p/488573" target="_blank" onclick="ga('send', 'event','發現頻道','列表_文章圖片','488573_HITACHI 日立 CM-N1000 冷凍收縮毛孔多功能美容儀');" class="picBox"> <img src="http://ym.zdmimg.com/201503/29/5517b316c0c752738.jpg_d200.jpg" alt="HITACHI 日立 CM-N1000 冷凍收縮毛孔多功能美容儀" title="" height= ''' req = urllib2.Request(url, headers = headers) try:html = urllib2.urlopen(req).read()pattern = re.compile(r"<a href=.* target=\"_blank\" onclick=.*\s?.*<img src=.*\.jpg\" alt=.*title=\"\".*height=") # correctres_list = pattern.findall(html)for content in res_list:pat = re.compile(r"http://.*p/\d{6}")url = pat.search(content).group()pat = re.compile(r"alt=\".*\" title")desc = pat.search(content).group()[5:-8]print url, re.sub(r"\s?", "", desc) except urllib2.HTTPError:print "failed parsing web url"執行結果為:
lming_08@ubuntu:~/MyWorkSpace/Pycode/htmlparse$ python get_smzdm_productinfo.py http://faxian.smzdm.com/fenlei/nvshixiangshui http://faxian.smzdm.com/p/487641 TOMMYHILFIGER都市新貴女士EDT淡香水30m http://faxian.smzdm.com/p/487231 GUERLAIN嬌蘭AquaAllegoria花草水語系列橙花伊甸園女士淡香 http://faxian.smzdm.com/p/482913 山東福利:LANCOME蘭蔻珍愛愛戀女士香水30m http://faxian.smzdm.com/p/479941 SalvatoreFerragamo菲拉格慕仲夏之夢淡香水噴霧100ml/3.4o http://faxian.smzdm.com/p/478681 VIVIENNEWESTWOODBoudoir密室女士香水(50ml http://faxian.smzdm.com/p/478055 SwissArmyMountainWater香 http://faxian.smzdm.com/p/475269 BURBERRY博柏利周末香水DEP50m http://faxian.smzdm.com/p/473353 MOSCHINO霧仙濃奧莉芙娃娃淡香水4.9m http://faxian.smzdm.com/p/472327 GALIMARD加利馬爾藍色妖姬綻放夏日限量版30m http://faxian.smzdm.com/p/471217 Dior迪奧真我淡香水50m http://faxian.smzdm.com/p/470015 BVLGARI寶格麗淡香水噴霧100m http://faxian.smzdm.com/p/469435 ANNASUI安娜蘇幻境綺緣女士持久淡香水50m http://faxian.smzdm.com/p/468123 CalvinKlein卡文克萊因為你女用淡香水100ml(簡裝 http://faxian.smzdm.com/p/467927 BURBERRY博柏利body肌體香水噴霧35M http://faxian.smzdm.com/p/467535 SalvatoreFerragamo菲拉格慕閃耀光采淡香水噴霧100m http://faxian.smzdm.com/p/467391 SalvatoreFerragamo菲拉格慕花水時刻淡香水噴霧100m http://faxian.smzdm.com/p/464821 BURBERRY博柏利周末香水噴霧50m http://faxian.smzdm.com/p/462473 Annasui安娜蘇搖滾心情淡香水噴霧50m http://faxian.smzdm.com/p/461755 LANVIN浪凡我愿意女士香水4.5m http://faxian.smzdm.com/p/461189 Lanvin浪凡光韻女士香水5m?
總結
以上是生活随笔為你收集整理的python 对指定URL获取其子链接的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python2爬虫学习系列教程
- 下一篇: Python ImportError: