Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/solr/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何为a href执行正则表达式? @KJR摄影_Python_Html_Regex_Post_Get - Fatal编程技术网

Python 如何为a href执行正则表达式? @KJR摄影

Python 如何为a href执行正则表达式? @KJR摄影,python,html,regex,post,get,Python,Html,Regex,Post,Get,所以我把它分开了一点,基本上我想找到“用户信息”在哪里,然后我需要知道a href是什么。在本例中,使用usertag类指定/kjr或span元素 如果有人能帮我,我很感激 到目前为止我已经知道了,但我知道这还不接近正确 <div class="user-info" style="position:absolute;top:5px;left:5px;z-index:1"> <a href="/kjrphotography" target="_blank"> <i

所以我把它分开了一点,基本上我想找到“用户信息”在哪里,然后我需要知道a href是什么。在本例中,使用usertag类指定/kjr或span元素

如果有人能帮我,我很感激

到目前为止我已经知道了,但我知道这还不接近正确

<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>
userdata=re.findall(ur“”,curlData)

使用HTML绝对是最好、最简单的选择,而
HTML
是您的HTML页面-以下是使用BeautifulSoup的示例:

userdata = re.findall(ur"<div class=\"user-info\"><\/div>",curlData)
您会发现,与将HTML作为字符串处理相比,它更简单、更健壮

或者:

from bs4 import BeautifulSoup
soup = BeautifulSoup(html)
print soup.select('.user-info a')[0]['href']
# /kjrphotography
从lxml导入html
xml=html.fragments\u fromstring(“”)
@KJR摄影
""")[0]
xml.find('span').text

返回
'@kjphotography'

我宁愿使用HTML解析库而不是正则表达式。例如,BeautifulSoup 4现在只是一个lxml包装器,所以您最好使用lxml directlyso,甚至使用正则表达式来获取usertag span字段?没人能给我这个?我使用的是curl,所以我更愿意保持这种方式。什么是完整的HTML?您可以使用Javascript或jQuery获取“用户信息”中的href。如果您想将href传递给python代码,那么您可以使用Ajax。如何在64位windows 7上安装它?
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
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