반복되는 질문 인 경우 사과하지만 SO 또는 내가 필요한 것을 처리하는 것으로 보이는 다른 질문을 찾을 수 없습니다. 여기 내 질문입니다 :XPath : 현재 노드 속성을 사용하여 현재 노드와 다음 노드의 텍스트를 선택하십시오.
this 웹 페이지의 정보를 얻으려면 scrapy
을 사용하고 있습니다.
<p class="titlestyle">ANT101H5 Introduction to Biological Anthropology and Archaeology
<span class='distribution'>(SCI)</span></p>
<span class='normaltext'>
Anthropology is the global and holistic study of human biology and behaviour, and includes four subfields: biological anthropology, archaeology, sociocultural anthropology and linguistics. The material covered is directed to answering the question: What makes us human? This course is a survey of biological anthropology and archaeology. [<span class='Helpcourse'
onMouseover="showtip(this,event,'24 Lectures')"
onMouseout="hidetip()">24L</span>, <span class='Helpcourse'
onMouseover="showtip(this,event,'12 Tutorials')"
onMouseout="hidetip()">12T</span>]<br>
<span class='title2'>Exclusion: </span><a href='javascript:OpenCourse("WEBCOURSENOTFOUND.html")'>ANT100Y5</a><br>
<span class='title2'>Prerequisite: </span><a href='javascript:OpenCourse("WEBCOURSEANT102H5.pl?fv=1")'>ANT102H5</a><br>
</span><br/><br/<br/>
거의 모든 페이지에 위의 코드 블록과 같습니다 명확하게하기 위해, 다음은 나에게 관심있는 해당 웹 페이지의 소스 코드의 블록입니다.
-
생물 인류학 및 고고학에
- ANT101H5 소개
- 제외 대상 : ANT100Y5
- 전제 조건 :이 모든에서 , 내가 잡아 필요 ANT102H5
문제는이다 Exclusion:
을 그 <span class="title2">
안에 있고 ANT100Y5
은 다음 안에 들어 있습니다. <a>
.
이 소스 코드에서 두 가지를 모두 포착 할 수없는 것 같습니다. 그것은 "당신이 다른 SO 질문을보고하지 않는 장님 경우에도,
hxs = HtmlXPathSelector(response)
sites = hxs.select("//*[(name() = 'p' and @class = 'titlestyle') or (name() = 'a' and @href and preceding-sibling::'//span/@class=title2')]")
내가이 어떤 도움을 주셔서 감사합니다 것 : 현재, 나는 보이는 ANT100Y5
을 잡기 위해 시도 (그리고 실패) 코드를 이 답변에 완벽하게 "(이 경우 자신이 투표를 닫을 것입니다). 나는 정말 많이 끝내 준다. 사전에
감사
편집 :
class regcalSpider(BaseSpider):
name = "disc"
allowed_domains = ['www.utm.utoronto.ca']
start_urls = ['http://www.utm.utoronto.ca/regcal/WEBLISTCOURSES1.html']
def parse(self, response):
items = []
hxs = HtmlXPathSelector(response)
sites = hxs.select("/*/p/text()[1] | \
(//span[@class='title2'])[1]/text() | \
(//span[@class='title2'])[1]/following-sibling::a[1]/text() | \
(//span[@class='title2'])[2]/text() | \
(//span[@class='title2'])[2]/following-sibling::a[1]/text()")
for site in sites:
item = RegcalItem()
item['title'] = site.select("a/text()").extract()
item['link'] = site.select("a/@href").extract()
item['desc'] = site.select("text()").extract()
items.append(item)
return items
filename = response.url.split("/")[-2]
open(filename, 'wb').write(response.body)
나에게이 결과를 제공합니다 :
나는 다음과 같은 코드를 사용하고 @Dimitre 제안 변경 후 전체 원본 코드
[{"title": [], "link": [], "desc": []},
{"title": [], "link": [], "desc": []},
{"title": [], "link": [], "desc": []}]
Th 제가 필요한 결과물이 아닙니까? 내가 도대체 뭘 잘못하고있는 겁니까? 앞서 언급 한대로이 스크립트는 this에 있습니다.
나는 어떻게 이러한 것들을 개별적으로 얻을 수 있겠지만 어떻게 3 가지를 모두 얻을 수있는 하나의 xpath select 문을 쓸 수 있을까? 이런 기본적인 질문을하는 것에 대해 유감 스럽지만, 이것은 xpath에 대한 첫 번째 진출입니다. – inspectorG4dget
@ inspectorG4dget : Simpy는 모든 문자열을 단일 문자열로 연결하기 위해'concat()'함수를 사용합니다 :'concat (ExprA, ExprB, ExprC)' –
@ Dimitre : 그러나 ExprA, ExprB 및 ExprC가 모두 하나의 문자열로 연결되는 경우 xpath는 어떻게 구별됩니까? – inspectorG4dget