我無法在頁面中找到一些文本。最大的部分是因爲文本的位置在頁面之間改變。關鍵字後收集線
如果我能得到一些幫助提取文本行的行之後包含關鍵字「營主任」
HTML例如:
<div class="span4">
<strong>Camp Director : </strong>
<span>Camp Directors Name</span>
</div>
我玩這個周圍:
def parse1(self, response):
hxs = Selector(response)
titles = hxs.xpath('//*[@id="fullwidth-container"]')
body = hxs.xpath('/html/body')
items = []
for titles in titles:
item = BayItem()
item["director"] = "".join(response.css('#fullwidth-container > div > div > div.geobase.complex-module-container.module > div.geobase-listing > div > div.premium.row-fluid.complex-module-columns-container > div.span8.respond-container.main-block > div.custom-field.geobase-cf-text > div:nth-child(4) > div:nth-child(3) > span').extract())
item["director1"] = titles.xpath('//*[@id="fullwidth-container"]/div/div/div[1]/div[1]/div/div[2]/div[1]/div[3]/div[3]/div[2]/span').extract()
item["director2"] = titles.xpath('//*[@id="fullwidth-container"]/div/div/div[1]/div[1]/div/div[2]/div[1]/div[4]/div[3]/div[2]/span').extract()
item["director3"] = titles.xpath('//*[@id="fullwidth-container"]/div/div/div[1]/div[1]/div/div[2]/div[1]/div[5]/div[4]/div[2]/span').extract()
item["director4"] = re.findall('Camp Director(\*)', response.body)
converter = html2text.HTML2Text()
converter.ignore_links = True
items.append(item)
return items
林有點傾向於我可能不得不使用更多的正則表達式,但我不完全確定如何使用它。所以,感謝幫助傢伙!
請嘗試:'re.findall('Camp Director。* \ n(。* \ n)',迴應。身體)' – Quinn