漂亮的网络搜索…得到的只是特克斯

2024-06-28 19:57:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我在试着提取

<a href="/reviews/28th-and-b-st-skatepark/">

    28th & B St Skatepark       #This is what I'm trying to grab, just the text.

</a>

用我的代码

^{pr2}$

我也会得到这样的回报。在

</a>, <a href="http://www.thrashermagazine.com/"><img alt="Thrasher Magazine Logo" src="/templates/HomePage/images/templatesImages/Header_logo.jpg" style="border:0px;"/></a>, <a href="javascript:void();" onclick="secondFunction();">Log in</a>, <a href="/Register/">Register</a>, <a href="http://www.thrashermagazine.com/"><span>Home</span></a>, <a href="http://shop.thrashermagazine.com"><span>Store</span></a>, <a href="/component/option,com_hwdvideoshare/Itemid,93/"><span>Thrasher Skateboard Magazine | Videos</span></a>, <a href="/tags/features/"><span>Features</span></a>, <a href="/component/option,com_jevents/Itemid,100/task,week.listevents/"><span>Thrasher Skateboard Magazine | Events</span></a>,

我知道这正是我要求我的脚本做的,但是我想知道是否有一种方法可以只获取我所指示的文本,而不是与标记相关的所有内容。在


Tags: andcomregisterhttpwwwcomponentoptionhref
1条回答
网友
1楼 · 发布于 2024-06-28 19:57:24

使用.text属性。e、 g组:

import urllib2
from BeautifulSoup import BeautifulSoup

url1 = "http://www.thrashermagazine.com/skateparks/search-results_m94/?cat=61&jr_state=CA&order=alpha&query=all"
content1 = urllib2.urlopen(url1).read()
soup = BeautifulSoup(content1)
print [e.text for e in soup.findAll('a')]

相关问题 更多 >