我正在尝试从网站上抓取数据,我需要文本标题。
[<a href="http://www.thegolfcourses.net/golfcourses/TX/38468.htm" rel="bookmark">Feather Bay Golf Course and Resort</a>]
[<a href="http://www.thegolfcourses.net/golfcourses/AZ/174830.htm" rel="bookmark">Paradise Valley Country Club</a>]
[<a href="http://www.thegolfcourses.net/golfcourses/IL/129935.htm" rel="bookmark">The Golf Club at Waters Edge</a>]
[<a href="http://www.thegolfcourses.net/golfcourses/NY/10630.htm" rel="bookmark">1000 Acres Ranch Resort</a>]
[<a href="http://www.thegolfcourses.net/golfcourses/VA/995731.htm" rel="bookmark">1757 Golf Club, 1757 Golf Club Front 9 Golf Course</a>]
[<a href="http://www.thegolfcourses.net/golfcourses/WI/320815.htm" rel="bookmark">27 Pines Golf Course</a>]
[<a href="http://www.thegolfcourses.net/golfcourses/WY/823145.htm" rel="bookmark">3 Creek Ranch Golf Club</a>]
[<a href="http://www.thegolfcourses.net/golfcourses/CA/18431.htm" rel="bookmark">3 Par At Four Points</a>]
[<a href="http://www.thegolfcourses.net/golfcourses/AZ/470720.htm" rel="bookmark">3 Parks Fairways</a>]
[<a href="http://www.thegolfcourses.net/golfcourses/IA/074920.htm" rel="bookmark">3-30 Golf & Country Club</a>]
我使用这段代码来处理它但是我很难写代码来提取任何关于如何去做的好主意吗?
import csv
import requests
from bs4 import BeautifulSoup
courses_list = []
for i in range(1):
url="http://www.thegolfcourses.net/page/{}?ls&location=California&orderby=title&radius=6750#038;location=California&orderby=title&radius=6750".format(i)
r = requests.get(url)
soup = BeautifulSoup(r.content)
g_data2=soup.find_all("article")
for item in g_data2:
try:
name= item.contents[5].find_all("a")
print name
except:
name=''
答案 0 :(得分:2)
使用string
属性
name= item.contents[5].find_all("a")[0].string
请记住findall
返回一个列表(ResultSet对象),所以如果你知道只有一个,你可以只查找该列表中的第0个索引。
或者您可以使用find
,如果您知道只有一个您感兴趣的结果。
name= item.contents[5].find("a").string
答案 1 :(得分:0)
如果我理解正确,这可行。 Is there InnerText equivalent in BeautifulSoup / python?
基本上尝试&#34; .text&#34;方法
name = item.contents[5].find_all("a").text
编辑:抱歉我无法正确格式化尝试这很糟糕但是有效
x = "<a> text </a>"
y = x.split(">")[1]
z = y.split("<")[0]
print z
text