123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- from urlparse import urljoin
- from urllib import urlencode
- from lxml import html
- from cgi import escape
- categories = ['social media']
- language_support = True
- base_url = 'https://twitter.com/'
- search_url = base_url+'search?'
- results_xpath = '//li[@data-item-type="tweet"]'
- link_xpath = './/small[@class="time"]//a'
- title_xpath = './/span[@class="username js-action-profile-name"]//text()'
- content_xpath = './/p[@class="js-tweet-text tweet-text"]//text()'
- def request(query, params):
- params['url'] = search_url + urlencode({'q': query})
-
- if params['language'] != 'all':
- params['cookies']['lang'] = params['language'].split('_')[0]
- return params
- def response(resp):
- results = []
- dom = html.fromstring(resp.text)
-
- for tweet in dom.xpath(results_xpath):
- link = tweet.xpath(link_xpath)[0]
- url = urljoin(base_url, link.attrib.get('href'))
- title = ''.join(tweet.xpath(title_xpath))
- content = escape(''.join(tweet.xpath(content_xpath)))
-
- results.append({'url': url,
- 'title': title,
- 'content': content})
-
- return results
|