我试图从我从网上刮下来的一个页面中提取数据,我发现这相当困难。我试过soup.get_Text()
,但效果不好,因为它只返回一行中的单个字符,而不是整个字符串对象。你知道吗
提取名称很容易,因为您可以使用'b'-tag
访问它,但例如提取街道(“Am Vogelwäldchen 2”)就相当困难。我可以试着用单个字符组合地址,但这似乎太复杂了,我觉得必须有一个更简单的方法。也许有人有更好的主意。哦,别介意奇怪的功能,我退回汤是因为我尝试了不同的方法。你知道吗
import urllib.request
import time
from bs4 import BeautifulSoup
#Performs a HTTP-'POST' request, passes it to BeautifulSoup and returns the result
def doRequest(request):
requestResult = urllib.request.urlopen(request)
soup = BeautifulSoup(requestResult)
return soup
def getContactInfoFromPage(page):
name = ''
straße = ''
plz = ''
stadt = ''
telefon = ''
mail = ''
url = ''
data = [
#'Name',
#'Straße',
#'PLZ',
#'Stadt',
#'Telefon',
#'E-Mail',
#'Homepage'
]
request = urllib.request.Request("http://www.altenheim-adressen.de/schnellsuche/" + page)
request.add_header("Content-Type", "application/x-www-form-urlencoded;charset=utf-8")
request.add_header("User-Agent", "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:33.0) Gecko/20100101 Firefox/33.0")
soup = doRequest(request)
#Save Name to data structure
findeName = soup.findAll('b')
name = findeName[2]
name = name.string.split('>')
data.append(name)
return soup
soup = getContactInfoFromPage("suche2.cfm?id=267a0749e983c7edfeef43ef8e1c7422")
print(soup.getText())
您可以依赖于字段标签来获取next sibling的文本。你知道吗
从中创建一个好的可重用函数将使其更加透明和易于使用:
用法:
相关问题 更多 >
编程相关推荐