鱼C论坛

 找回密码
 立即注册
查看: 3729|回复: 3

爬虫问题,大神请进,问题解决发鱼币

[复制链接]
发表于 2017-1-29 12:13:04 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-80-8370b67ec7ea> in <module>()
      3 header=soup1.select('a')
      4 for a in header:
----> 5     print(a['href'])
      6
      7

/Users/ylf__/Downloads/python/anaconda/lib/python3.5/site-packages/bs4/element.py in __getitem__(self, key)
    995         """tag[key] returns the value of the 'key' attribute for the tag,
    996         and throws an exception if it's not there."""
--> 997         return self.attrs[key]
    998
    999     def __iter__(self):

KeyError: 'href'

请问这个错误的原因是什么?
我用的是jupyter,爬取淘宝网的列表页面的网址
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2017-1-29 12:13:51 | 显示全部楼层
import requests
res=requests.get('https://list.tmall.com/search_shopitem.htm?user_id=1832335918&cat=2&spm=875.7931836.a2227oh.d100&oq=everite%C6%EC%BD%A2%B5%EA&suggest=0_1&from=mallfp..pc_1_suggest&ds=1&stype=search')
print(res.text)
from bs4 import BeautifulSoup
url_html=res.text
soup1=BeautifulSoup(url_html,'html.parser')
header=soup1.select('a')
for a in header:
    print(a['href'])
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-1-29 13:03:28 | 显示全部楼层
import requests
res=requests.get('https://list.tmall.com/search_shopitem.htm?user_id=1832335918&cat=2&spm=875.7931836.a2227oh.d100&oq=everite%C6%EC%BD%A2%B5%EA&suggest=0_1&from=mallfp..pc_1_suggest&ds=1&stype=search')

from bs4 import BeautifulSoup
url_html=res.text
soup1=BeautifulSoup(url_html,'html.parser')
for product in soup1.select('.product'):
    title=product.select('.productTitle')[0].text
    shop_name=product.select('.productShop-name')[0].text
    price=product.select('em')[0].text
    count=product.select('em')[1].text
    comment=product.select('span')[1].text
    #print(product.select('a'))
    print(title,shop_name,price,count,comment)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-1-29 13:05:18 | 显示全部楼层
自己琢磨了一下解决了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-24 21:18

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表