0
<div class="user-info" style="position:absolute;top:5px;left:5px;z-index:1">

<a href="/kjrphotography" target="_blank">

<img class="photo_user" src="http://images.ak.instagram.com/profiles/profile_507998691_75sq_1376497436.jpg" width="40" height="40" title="kjrphotography" border="0">

</a>

<span class="usertag" style="display: none;">@kjrphotography</span>

</div>

所以我有点分开了这个,基本上我试图找到“用户信息”在哪里,然后我需要抓住 a href 是什么。在这种情况下 /kjrphotography 或带有 usertag 类的 span 元素。

如果有人可以帮助我,请不胜感激。

到目前为止我有这个,但我知道这不是正确的

userdata = re.findall(ur"<div class=\"user-info\"><\/div>",curlData)
4

2 回答 2

0

绝对是最好和最简单的使用 HTML,html作为您的 HTML 页面 - 这是一个使用 BeautifulSoup 的示例:

from bs4 import BeautifulSoup
soup = BeautifulSoup(html)
print soup.select('.user-info a')[0]['href']
# /kjrphotography

您会发现它比尝试将 HTML 视为字符串更容易且更健壮...

或者:

for info in soup.find_all('div', class_='user-info'):
    print 'href:', info.find('a', href=True)['href']
    print 'user:', info.find('span', class_='usertag').text

#href: /kjrphotography
#user: @kjrphotography
于 2013-10-18T07:12:33.247 回答
0
from lxml import html
xml = html.fragments_fromstring("""<div class="user-info" style="position:absolute;top:5px;left:5px;z-index:1">

<a href="/kjrphotography" target="_blank">

<img class="photo_user" src="http://images.ak.instagram.com/profiles/profile_507998691_75sq_1376497436.jpg" width="40" height="40" title="kjrphotography" border="0">

</a>

<span class="usertag" style="display: none;">@kjrphotography</span>

</div>""")[0]

xml.find('span').text

返回'@kjphotography'

于 2013-10-18T07:13:32.910 回答