def test_parse_url(self): test_url = 'http://r.search.yahoo.com/_ylt=A0LEb9JUSKcAEGRXNyoA;_ylu=X3oDMTEzZm1qazYwBHNlYwNzcgRwb3MDMQRjb' +\ '2xvA2Jm2dGlkA1NNRTcwM18x/RV=2/RE=1423106085/RO=10/RU=https%3a%2f%2fthis.is.the.url%2f/RK=0/RS=' +\ 'dtcJsfP4mEeBOjnVfUQ-' url = yahoo.parse_url(test_url) self.assertEqual('https://this.is.the.url/', url) test_url = 'http://r.search.yahoo.com/_ylt=A0LElb9JUSKcAEGRXNyoA;_ylu=X3oDMTEzZm1qazYwBHNlYwNzcgRwb3MDMQRjb' +\ '2xvA2Jm2dGlkA1NNRTcwM18x/RV=2/RE=1423106085/RO=10/RU=https%3a%2f%2fthis.is.the.url%2f/RS=' +\ 'dtcJsfP4mEeBOjnVfUQ-' url = yahoo.parse_url(test_url) self.assertEqual('https://this.is.the.url/', url) test_url = 'https://this.is.the.url/' url = yahoo.parse_url(test_url) self.assertEqual('https://this.is.the.url/', url)
def response(resp): results = [] dom = html.fromstring(resp.text) # parse results for result in dom.xpath(results_xpath): urls = result.xpath(url_xpath) if len(urls) != 1: continue url = sanitize_url(parse_url(extract_url(urls, search_url))) title = extract_text(result.xpath(title_xpath)[0]) content = extract_text(result.xpath(content_xpath)[0]) # parse publishedDate publishedDate = extract_text(result.xpath(publishedDate_xpath)[0]) # still useful ? if re.match("^[0-9]+ minute(s|) ago$", publishedDate): publishedDate = datetime.now() - timedelta( minutes=int(re.match(r'\d+', publishedDate).group())) elif re.match("^[0-9]+ days? ago$", publishedDate): publishedDate = datetime.now() - timedelta( days=int(re.match(r'\d+', publishedDate).group())) elif re.match("^[0-9]+ hour(s|), [0-9]+ minute(s|) ago$", publishedDate): timeNumbers = re.findall(r'\d+', publishedDate) publishedDate = datetime.now()\ - timedelta(hours=int(timeNumbers[0]))\ - timedelta(minutes=int(timeNumbers[1])) else: try: publishedDate = parser.parse(publishedDate) except: publishedDate = datetime.now() if publishedDate.year == 1900: publishedDate = publishedDate.replace(year=datetime.now().year) # append result results.append({ 'url': url, 'title': title, 'content': content, 'publishedDate': publishedDate }) # return results return results
def response(resp): results = [] dom = html.fromstring(resp.text) for result in dom.xpath(results_xpath): url = parse_url(extract_url(result.xpath(url_xpath), search_url)) title = extract_text(result.xpath(title_xpath)[0]) content = extract_text(result.xpath(content_xpath)[0]) results.append({'url': url, 'title': title, 'content': content}) if not suggestion_xpath: return results for suggestion in dom.xpath(suggestion_xpath): results.append({'suggestion': extract_text(suggestion)}) return results
def response(resp): results = [] dom = html.fromstring(resp.text) # parse results for result in eval_xpath_list( dom, '//ol[contains(@class,"searchCenterMiddle")]//li'): url = eval_xpath_getindex(result, './/h4/a/@href', 0, None) if url is None: continue url = parse_url(url) title = extract_text(result.xpath('.//h4/a')) content = extract_text(result.xpath('.//p')) img_src = eval_xpath_getindex(result, './/img/@data-src', 0, None) item = { 'url': url, 'title': title, 'content': content, 'img_src': img_src } pub_date = extract_text( result.xpath('.//span[contains(@class,"s-time")]')) ago = AGO_RE.search(pub_date) if ago: number = int(ago.group(1)) delta = AGO_TIMEDELTA[ago.group(2)] pub_date = datetime.now() - delta * number else: try: pub_date = parser.parse(pub_date) except parser.ParserError: pub_date = None if pub_date is not None: item['publishedDate'] = pub_date results.append(item) for suggestion in eval_xpath_list( dom, '//div[contains(@class,"AlsoTry")]//td'): results.append({'suggestion': extract_text(suggestion)}) return results
def response(resp): results = [] dom = html.fromstring(resp.text) # parse results for result in dom.xpath(results_xpath): urls = result.xpath(url_xpath) if len(urls) != 1: continue url = sanitize_url(parse_url(extract_url(urls, search_url))) title = extract_text(result.xpath(title_xpath)[0]) content = extract_text(result.xpath(content_xpath)[0]) # parse publishedDate publishedDate = extract_text(result.xpath(publishedDate_xpath)[0]) # still useful ? if re.match("^[0-9]+ minute(s|) ago$", publishedDate): publishedDate = datetime.now() - timedelta(minutes=int(re.match(r'\d+', publishedDate).group())) elif re.match("^[0-9]+ days? ago$", publishedDate): publishedDate = datetime.now() - timedelta(days=int(re.match(r'\d+', publishedDate).group())) elif re.match("^[0-9]+ hour(s|), [0-9]+ minute(s|) ago$", publishedDate): timeNumbers = re.findall(r'\d+', publishedDate) publishedDate = datetime.now()\ - timedelta(hours=int(timeNumbers[0]))\ - timedelta(minutes=int(timeNumbers[1])) else: try: publishedDate = parser.parse(publishedDate) except: publishedDate = datetime.now() if publishedDate.year == 1900: publishedDate = publishedDate.replace(year=datetime.now().year) # append result results.append({'url': url, 'title': title, 'content': content, 'publishedDate': publishedDate}) # return results return results
def response(resp): results = [] dom = html.fromstring(resp.text) for result in dom.xpath(results_xpath): url = parse_url(extract_url(result.xpath(url_xpath), search_url)) title = extract_text(result.xpath(title_xpath)[0]) content = extract_text(result.xpath(content_xpath)[0]) publishedDate = extract_text(result.xpath(publishedDate_xpath)[0]) if re.match("^[0-9]+ minute(s|) ago$", publishedDate): publishedDate = datetime.now() - timedelta(minutes=int(re.match(r'\d+', publishedDate).group())) # noqa else: if re.match("^[0-9]+ hour(s|), [0-9]+ minute(s|) ago$", publishedDate): timeNumbers = re.findall(r'\d+', publishedDate) publishedDate = datetime.now()\ - timedelta(hours=int(timeNumbers[0]))\ - timedelta(minutes=int(timeNumbers[1])) else: # TODO year in string possible? publishedDate = datetime.strptime(publishedDate, "%b %d %H:%M%p") if publishedDate.year == 1900: publishedDate = publishedDate.replace(year=datetime.now().year) results.append({'url': url, 'title': title, 'content': content, 'publishedDate': publishedDate}) if not suggestion_xpath: return results for suggestion in dom.xpath(suggestion_xpath): results.append({'suggestion': extract_text(suggestion)}) return results
def response(resp): results = [] dom = html.fromstring(resp.text) for result in dom.xpath(results_xpath): url = parse_url(extract_url(result.xpath(url_xpath), search_url)) title = extract_text(result.xpath(title_xpath)[0]) content = extract_text(result.xpath(content_xpath)[0]) publishedDate = extract_text(result.xpath(publishedDate_xpath)[0]) if re.match("^[0-9]+ minute(s|) ago$", publishedDate): publishedDate = datetime.now() - timedelta(minutes=int(re.match(r'\d+', publishedDate).group())) # noqa else: if re.match("^[0-9]+ hour(s|), [0-9]+ minute(s|) ago$", publishedDate): timeNumbers = re.findall(r'\d+', publishedDate) publishedDate = datetime.now()\ - timedelta(hours=int(timeNumbers[0]))\ - timedelta(minutes=int(timeNumbers[1])) else: publishedDate = parser.parse(publishedDate) if publishedDate.year == 1900: publishedDate = publishedDate.replace(year=datetime.now().year) results.append({'url': url, 'title': title, 'content': content, 'publishedDate': publishedDate}) if not suggestion_xpath: return results for suggestion in dom.xpath(suggestion_xpath): results.append({'suggestion': extract_text(suggestion)}) return results